I have create custom post(book). In which i have five texonomy categories: CatA, CatB, CatC, CatD, CatE. & i want to show only 3 posts and i want that these posts coming from CatA, CatC, CatD.(One post each these categories.)


        $args = array(
            'post_type' => 'book',
            'posts_per_page' => '3',
            $terms = get_terms( array(
            'taxonomy' => 'categories',
            'field' => 'name',
            'terms' => array('CatA', 'CatB', 'CatC')

        $query = new WP_Query( $args );

        if ( $query->have_posts() ) { ?>
            <?php while ( $query->have_posts() ) { $query->the_post(); ?>
                <div style="background-image:url('<?php echo get_the_post_thumbnail_url(''`enter code here`);?>')">

                <?php the_title();?>

            <?php } // end while ?>

        <?php } wp_reset_postdata(); ?>
  • Can you please also post a sample of the results of this query? Commented Sep 26, 2019 at 8:25
  • You will need to loop through the categories and separately query one post from each. Commented Sep 26, 2019 at 8:27

2 Answers 2


You probably need to use a taxonomy query rather than the terms field (I don't see terms as a valid parameter on WordPress's WP_Query documentation page). This stackexchange question and answer should provide you with the necessary code to do what you are trying to do.

If, however, the categories are not custom taxonomies (or really are just categories), then you might be able to use one of the approaches here.


Please Try below code to get only one custom post from each terms. replace terms ids with your term ids in $array_term = array(16,21);

    # pass your term IDS
    $array_term = array(16,21); 
    $terms_array = array( 
      'taxonomy' => 'categories',// you can change it according to your taxonomy
      'include' => $array_term,
      'parent'   => 0 // If parent => 0 is passed, only top-level terms will be returned

    $categories_terms = get_terms($terms_array); 
    foreach($categories_terms as $categorie_term): ?>
        <h4><?php echo $categorie_term->name; ?></h4>
        $post_args = array(
              'posts_per_page' => 1,
              'post_type' => 'book', // you can change it according to your custom post type
              'tax_query' => array(
                      'taxonomy' => 'categories', // you can change it according to your taxonomy
                      'field' => 'term_id', // this can be 'term_id', 'slug' & 'name'
                      'terms' => $categorie_term->term_id,
        $query = new WP_Query($post_args); 
            if ( $query->have_posts() ) 
                while ( $query->have_posts() ) 
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    endforeach; // End Term foreach; 

Let me know if this works for you

  • Why are you going through all the effort of getting the terms with get_terms when you already have the IDs? Commented Sep 26, 2019 at 13:42
  • i have gave just an idea how to achieve his need, i have displayed terms name using get_terms. i know that i have terms ids. i have used get_terms just for reference. Commented Sep 26, 2019 at 14:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.