如何在多维数组中获取post数据形式的WP_Query数组?

How to get post data form WP_Query array in multidimensional array?

如何从 object(WP_Query) 中获取 $value 数组值?

post_content、ID、页面名称、类别等

这是我的代码:


    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 对于 $value 数组:

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.

尝试确保 $master 是一个多维数组,然后再分配 $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;
            
}

如果这个问题与this question of yours相关,而您需要post这种格式的标题Year > Months > Title of each post,那么您可以使用以下代码:

$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>";
}

请注意,我使用了 post object 中的 post_title。这将 return 以下输出:

2021:
  January:
    1.Post Title
    2.Post Title
  March:
    3.Post Title

2020:
  May:
    4.Post Title

当您使用 get_posts 时,它将 return post objects。每个 object 包括以下内容:

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] =>
)

在上面的代码中,我在 $master[$year][$mon][] = $post->post_title.

这一行访问了 $post->post_title