0

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.)

<?php

        $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`);?>')">
                </div>

                <?php the_title();?>

            <?php } // end while ?>

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

2 Answers 2

0

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.

-1

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>
        <?php 
        $post_args = array(
              'posts_per_page' => 1,
              'post_type' => 'book', // you can change it according to your custom post type
              'tax_query' => array(
                  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() ) 
                {
                    $query->the_post(); 
                    ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                     </li>
                    <?php
                }
           }
         wp_reset_postdata(); 
    endforeach; // End Term foreach; 

Let me know if this works for you

2
  • Why are you going through all the effort of getting the terms with get_terms when you already have the IDs? 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. Sep 26, 2019 at 14:15

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.

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