skip to Main Content

How can I get $value array values from object(WP_Query)?

post_content, ID, pagename, category etc.

Here is my code:


    global $post;
    
    $args = array(
        'post_type' => 'history',
        'order'     => 'ASC'
    );
    
    $master = [];
        
    $post_query = new WP_Query($args);
    
    if ($post_query->have_posts() ) {
                
        while ($post_query->have_posts()) {
                
            $post_query->the_post();
            
            /* Get Post Date */
            $year = get_the_date( 'Y' );
            $mon = get_the_date( 'M' );
                
            $master[$year][$mon][] = $post_query;
            
        }
        
        foreach ($master as $masterkey => $mastervalue) {
            echo "<p>";

            echo "masterkey: ".  $masterkey . "<br>";

            foreach($mastervalue as $key => $value) {
                echo "key: " . $key . "<br>";
                echo "value: " . $value . "<br>";
                                        
            }
            
            echo "</p>";
        }
        
    }

wp_reset_postdata();

var_dump for $value array:

array(1) { [0]=> object(WP_Query)#2459 (51) { ["query"]=> array(2) { ["post_type"]=> string(7) "history" ["order"]=> string(3) "ASC" } ["query_vars"]=> array(63) { ["post_type"]=> string(7) "history" ["order"]=> string(3) "ASC" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(0) "" ["tag_id"]=> string(0) "" ["author"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["title"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["embed"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["post_name__in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["post_parent__in"]=> array(0) { } ["post_parent__not_in"]=> array(0) { } ["author__in"]=> array(0) { } ["author__not_in"]=> array(0) { } ["ignore_sticky_posts"]=> bool(false) ["suppress_filters"]=> bool(false) ["cache_results"]=> bool(true) ["update_post_term_cache"]=> bool(true) ["lazy_load_term_meta"]=> bool(true) ["update_post_meta_cache"]=> bool(true) ["posts_per_page"]=> int(10) ["nopaging"]=> bool(false) ["comments_per_page"]=> string(2) "50" ["no_found_rows"]=> bool(false) } ["tax_query"]=> object(WP_Tax_Query)#2465 (6) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" ["table_aliases":protected]=> array(0) { } ["queried_terms"]=> array(0) { } ["primary_table"]=> string(11) "pkctU_posts" ["primary_id_column"]=> string(2) "ID" } ["meta_query"]=> object(WP_Meta_Query)#2452 (9) { ["queries"]=> array(0) { } ["relation"]=> NULL ["meta_table"]=> NULL ["meta_id_column"]=> NULL ["primary_table"]=> NULL ["primary_id_column"]=> NULL ["table_aliases":protected]=> array(0) { } ["clauses":protected]=> array(0) { } ["has_or_relation":protected]=> bool(false) } ["date_query"]=> bool(false) ["request"]=> string(295) "SELECT SQL_CALC_FOUND_ROWS pkctU_posts.ID FROM pkctU_posts WHERE 1=1 AND pkctU_posts.post_type = 'history' AND (pkctU_posts.post_status = 'publish' OR pkctU_posts.post_status = 'acf-disabled' OR pkctU_posts.post_status = 'dp-rewrite-republish') ORDER BY pkctU_posts.post_date ASC LIMIT 0, 10" ["posts"]=> array(8) { [0]=> object(WP_Post)#2458 (24) { ["ID"]=> int(3786) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-01-24 18:34:38" ["post_date_gmt"]=> string(19) "2016-01-24 15:34:38" ["post_content"]=> string(1937) "
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

2

Answers


  1. Try to ensure $master is a multi-dimensional array before assigning values like $master[$year][$mon][] = $post_query;

    while ($post_query->have_posts()) {
                    
        $post_query->the_post();
                
        /* Get Post Date */
        $year = get_the_date( 'Y' );
        $mon = get_the_date( 'M' );
        
        if (!is_array($master[$year]) {
            $master[$year] = [];
        }
        if (!is_array($master[$year][$mon]) {
            $master[$year][$mon] = [];
        }
        $master[$year][$mon][] = $post_query;
                
    }
    
    Login or Signup to reply.
  2. If this question is related to this question of yours, and you need the title of each post in this format Year > Months > Title of each post, then you could use the following code:

    $posts = get_posts(array(
      'post_type' => 'history',
      'posts_per_page' => -1,
      'orderby'   => 'date'
    ));
    
    $master = [];
    
    foreach ($posts as $post) {
      setup_postdata($post);
    
      $time = strtotime($post->post_date);
      $year = date('Y', $time);
      $mon = date('F', $time);
    
      $master[$year][$mon][] = $post->post_title;
    }
    
    wp_reset_postdata();
    
    
    foreach ((array)$master as $yearly => $yvalue) {
      echo $yearly . ": <br>";
      echo "<ul>";
      foreach ($yvalue as $monthly => $mvalue) {
        echo "<li>" . $monthly . ": </li>";
        echo "<ul>";
        foreach ($mvalue as $post_title_k => $post_title_v) {
          echo "<li>" . $post_title_v . "</li>";
        }
        echo "</ul>";
      }
      echo "</ul>";
    }
    

    Notice that i used post_title from the post object. Which will return the following output:

    2021:
      January:
        1.Post Title
        2.Post Title
      March:
        3.Post Title
    
    2020:
      May:
        4.Post Title
    

    When you use get_posts it will return post objects. Each object includes the followings:

    WP_Post Object
    (
        [ID] =>
        [post_author] =>
        [post_date] => 
        [post_date_gmt] => 
        [post_content] => 
        [post_title] => 
        [post_excerpt] => 
        [post_status] =>
        [comment_status] =>
        [ping_status] => 
        [post_password] => 
        [post_name] =>
        [to_ping] => 
        [pinged] => 
        [post_modified] => 
        [post_modified_gmt] =>
        [post_content_filtered] => 
        [post_parent] => 
        [guid] => 
        [menu_order] =>
        [post_type] =>
        [post_mime_type] => 
        [comment_count] =>
        [filter] =>
    )
    

    In the code above i accessed $post->post_title in this line $master[$year][$mon][] = $post->post_title.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search