$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>one post per term taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

one post per term taxonomy

matteradmin11PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far