且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从多维数组中的 WP_Query 数组中获取发布数据?

更新时间:2023-11-30 22:54:28

如果此问题与 你的这个问题,你需要以这种格式的每篇文章的标题Year >月 >每个帖子的标题,那么你可以使用以下代码:

$posts = get_posts(array('post_type' =>'历史','posts_per_page' =>-1,'orderby' =>'日期'));$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) {回声 $yearly .":<br>>回声<ul>";foreach ($yvalue as $monthly => $mvalue) {echo "<li>".$每月.:</li>";回声<ul>";foreach ($mvalue as $post_title_k => $post_title_v) {echo "<li>".$post_title_v .</li>";}echo "</ul>";}echo "</ul>";}

请注意,我使用了 post 对象 中的 post_title.这将返回以下输出:

2021:一月:1.帖子标题2.帖子标题行进:3.帖子标题2020:可能:4.帖子标题

当您使用 get_posts 时,它将返回 post 对象.每个对象包括以下内容:

WP_Post 对象([ID] =>[post_author] =>[post_date] =>[post_date_gmt] =>[post_content] =>[post_title] =>[post_excerpt] =>[post_status] =>[评论状态] =>[ping_status] =>[post_password] =>[post_name] =>[to_ping] =>[ping] =>[后修改] =>[post_modified_gmt] =>[post_content_filtered] =>[post_parent] =>[指南] =>[menu_order] =>[post_type] =>[post_mime_type] =>[评论计数] =>[过滤器] =>)

在上面的代码中,我在这一行中访问了 $post->post_title $master[$year][$mon][] = $post->post_title.

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.

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.