admin管理员组文章数量:1289834
I have 2 custom post types:
- Drama
- Episode
Drama is Parent, and Episode is child. Now, for instance I add 2 dramas and and then add episodes (Drama 1, Drama 2 as Parent):
Drama 1
- --- Episode 1
- --- Episode 2 ** updated
- --- Episode 3
- --- Episode 4 ** updated
Drama 2
- --- Episode 1
- --- Episode 2
- --- Episode 3
- --- Episode 4
- --- Episode 5
- --- Episode 6
- --- Episode 7 ** updated
- --- Episode 8 ** updated
** updated shows the episode was recently updated.
On Homepage, I want to display all posts from "Episode" post type order by post_modified. So with above example it shows the following on homepage:
Sorted by post_modified..
- Episode 8 - Drama 2
- Episode 7 - Drama 2
- Episode 2 - Drama 1
- Episode 4 - Drama 1
- Episode 1 - Drama 1
- Episode 3 - Drama 1
- Episode 1 - Drama 2 .. .. ...
It displays all 12 episodes ordered by post_modified.
THE PROBLEM..
Since I have only 2 Dramas, I want to display only 2 most recently updated episodes from those dramas (only 1 post from every parent), I dont want to show all episodes. So the desired output on homepage should be only following 2 episodes:
- Episode 8 - Drama 2
- Episode 4 - Drama 1
I used WP_Query to display episodes, however it displays all posts. How can I limit episodes and get only 1 most recently updated episode from each Drama.
$wp_query = new WP_Query( array(
'post_type' => 'episode',
'orderby' => 'post_modified',
'order' => 'DESC',
'suppress_filters' => true,
'posts_per_page' => 100,
'paged' => $paged
));
Edit - Update: Rephrased and removed another similar question.
Edit 2: The number of Dramas and Episodes are just for example, the actual number of Dramas is over 20K and Episodes over 300K.
I have 2 custom post types:
- Drama
- Episode
Drama is Parent, and Episode is child. Now, for instance I add 2 dramas and and then add episodes (Drama 1, Drama 2 as Parent):
Drama 1
- --- Episode 1
- --- Episode 2 ** updated
- --- Episode 3
- --- Episode 4 ** updated
Drama 2
- --- Episode 1
- --- Episode 2
- --- Episode 3
- --- Episode 4
- --- Episode 5
- --- Episode 6
- --- Episode 7 ** updated
- --- Episode 8 ** updated
** updated shows the episode was recently updated.
On Homepage, I want to display all posts from "Episode" post type order by post_modified. So with above example it shows the following on homepage:
Sorted by post_modified..
- Episode 8 - Drama 2
- Episode 7 - Drama 2
- Episode 2 - Drama 1
- Episode 4 - Drama 1
- Episode 1 - Drama 1
- Episode 3 - Drama 1
- Episode 1 - Drama 2 .. .. ...
It displays all 12 episodes ordered by post_modified.
THE PROBLEM..
Since I have only 2 Dramas, I want to display only 2 most recently updated episodes from those dramas (only 1 post from every parent), I dont want to show all episodes. So the desired output on homepage should be only following 2 episodes:
- Episode 8 - Drama 2
- Episode 4 - Drama 1
I used WP_Query to display episodes, however it displays all posts. How can I limit episodes and get only 1 most recently updated episode from each Drama.
$wp_query = new WP_Query( array(
'post_type' => 'episode',
'orderby' => 'post_modified',
'order' => 'DESC',
'suppress_filters' => true,
'posts_per_page' => 100,
'paged' => $paged
));
Edit - Update: Rephrased and removed another similar question.
Edit 2: The number of Dramas and Episodes are just for example, the actual number of Dramas is over 20K and Episodes over 300K.
Share Improve this question edited Jul 6, 2021 at 16:59 Themer asked Jun 30, 2021 at 5:32 ThemerThemer 213 bronze badges1 Answer
Reset to default 0I think maybe you don't need Wp_Query. I believe you can just do
$episodes_for_front_page = array();
//get the two dramas
$dramas = get_posts(array('post_type' => 'drama'));
foreach ($dramas as $drama){
//get all the episodes belonging to that drama, sorted by most recent update
$episodes = get_posts(
array(
'post_type' => 'episode',
'post_parent' => $drama->ID,
'orderby' => 'post_modified',
'order' => 'DESC'
)
);
//add the most recent episode to the list of posts to display
$episodes_for_front_page[] = $episodes[0];
}
本文标签: wp queryGet only 1 Most Recently Modified Child Post from Parent
版权声明:本文标题:wp query - Get only 1 Most Recently Modified Child Post from Parent 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741461158a2380046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论