one post per term taxonomy

admin2025-06-03  4

I need some help, again.

I use this query:

$tax = get_the_terms($id, 'coupon_category');
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        'tax_query' => array( 
                        'relation' => 'AND',
                            array(
                            'taxonomy' => 'coupon_category', // Taxonomy
                            'field'    => 'slug',
                            'terms'    => $tax[0]->slug, // Your category slug
                            ),
                            array(
                            'taxonomy' => APP_TAX_STORE,
                            'field'    => 'slug',
                            'terms'    => $term->slug,
                            'operator' => 'NOT IN',
                            ),
                        ),                      
                    ) );

I know need a solution to only show one post from each Taxonomy term APP_TAX_STORE. Tried some solutions from this userfull forum but non worked for me.

I need some help, again.

I use this query:

$tax = get_the_terms($id, 'coupon_category');
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                        'tax_query' => array( 
                        'relation' => 'AND',
                            array(
                            'taxonomy' => 'coupon_category', // Taxonomy
                            'field'    => 'slug',
                            'terms'    => $tax[0]->slug, // Your category slug
                            ),
                            array(
                            'taxonomy' => APP_TAX_STORE,
                            'field'    => 'slug',
                            'terms'    => $term->slug,
                            'operator' => 'NOT IN',
                            ),
                        ),                      
                    ) );

I know need a solution to only show one post from each Taxonomy term APP_TAX_STORE. Tried some solutions from this userfull forum but non worked for me.

Share Improve this question asked Feb 3, 2019 at 15:15 joloshopjoloshop 211 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Yeah sure.

<?php 
                    $included_post_ids =array();
                    $args = array(
                      'post_type' => 'APP_POST_TYPE',
                      'post_status' => 'publish',
                      'posts_per_page' => 1,
                      'tax_query' => array( 

                          array(
                          'taxonomy' => 'coupon_category', // Taxonomy
                          'field'    => 'slug',
                          'terms'    => $tax[0]->slug, // Your category slug
                          ),
                    ),
                  );
                  $custom_posts = get_posts( $args );
                  if( !empty( $custom_posts ) ){
                    foreach ($custom_posts as $key => $postObj) {
                      $included_post_ids[] = $postObj->ID;
                    }
                  }
                  //Similar proceduce for other taxonomies 
                  $args = array(
                    'post__in' => $included_post_ids,
                    'post_type' => 'APP_POST_TYPE',
                  )
                  $final_posts = new WP_Query( $args );
                  if ( $final_posts->have_posts() ) {
                  }

?>

You can't get the only one post of each taxonomy in a single query. You have to get the single post by using posts_per_page=> 1 for each term and then use the post__in to get the one post of each taxonomy.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748947298a315063.html

最新回复(0)