最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get only one custom post from each category

programmeradmin0浏览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(); ?>

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(); ?>
Share Improve this question edited Sep 26, 2019 at 7:38 Arun 31 bronze badge asked Sep 26, 2019 at 5:53 ArunArun 11 bronze badge 2
  • Can you please also post a sample of the results of this query? – Ted Stresen-Reuter Commented Sep 26, 2019 at 8:25
  • You will need to loop through the categories and separately query one post from each. – Jacob Peattie Commented Sep 26, 2019 at 8:27
Add a comment  | 

2 Answers 2

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论