Hello Guys i am trying to customize a wordpress theme and am getting this is How it queries All Posts with episodes post_type.

My Question Is.

this Post type is related to another post_type which is called tvshows so each post in that is post_type epsidoes has a meta_field called series which specifies the tvshow this postis related to.

for example There is series one post which is post_type = tvshows it's id is 20

and there are 2 posts in post_type = episodes, both these posts have meta_feild called series and both are related to the tvshow above so they have there meta_field series=20 which is the id of tvshow post type.

The problem in my query below is it shows all post in the post type episodes and i want it to show related posts once for example if 2 posts which are epsidoes share 1 tvshow i want to show them once like on database we use group by right but then i want to show the latest by date or name

so basically am saying i want to display all episode posts but latest once group by meta field series or show post which have same meta field once

// Compose Query
$query = array(
    'post_type' => array('episodes'),
    'showposts' => 10,
    'orderby'   => 'date',
    'order'     => 'DESC'
<div <?php if($slid) echo 'id="dt-episodes" '; ?>class="animation-2 items <?php echo $maxwidth; ?>">
    <?php query_posts($query); while(have_posts()){ the_post(); get_template_part('inc/parts/item_ep'); } wp_reset_query(); ?>
  • you should avoid using query_posts, use WP_Query instead for secondary loops, and pre_get_posts for the main loop. Both have better documentation, fewer pitfalls, and are easier to debug. There are very few times that query_posts is appropriate, and as a plugin or theme developer it's not worth the trouble to use it
    – Tom J Nowell
    Jul 8, 2022 at 16:44
  • Ok but how do i group them by meta_field series and order them by date? Jul 8, 2022 at 16:46
  • take a look at the examples on developer.wordpress.org/reference/classes/wp_query, and the custom field section developer.wordpress.org/reference/classes/wp_query/… as well as the pre_get_posts filter that lets you modify the parameters of the main query eliminating the need for a second query and improving performance developer.wordpress.org/reference/hooks/pre_get_posts. Also keep in mind that these are comments on the question, I have not posted an answer/solution, the answers appear below in larger text with voting controls
    – Tom J Nowell
    Jul 8, 2022 at 16:46


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.