one post per term taxonomy

时间:2019-02-03 作者:joloshop

我再次需要一些帮助。

我使用此查询:

$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\',
                            ),
                        ),                      
                    ) );
我知道需要一个解决方案,只显示每个分类术语中的一篇文章APP_TAX_STORE. 尝试了一些来自这个userfull论坛的解决方案,但对我来说不起作用。

2 个回复
SO网友:Mohsin Ghouri

是的,当然。

<?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() ) {
                  }
?>

SO网友:Mohsin Ghouri

您无法在单个查询中获得每个分类法的唯一一篇文章。你必须使用posts_per_page=> 1 对于每个术语,然后使用post__in 获取每个分类法的一个帖子。

相关推荐

WP_SET_POST_TERMS()似乎非常慢

正常吗wp_set_post_terms() 非常慢?什么时候wp_insert_post() 大约需要1秒。添加4wp_set_post_terms() 操作前7-10秒。伪代码示例:$values = [\'value_1\', \'value_2\', \'value_3\', \'value_4\', \'value_5\', \'value_6\']; $post_id = \'1234\'; $taxonomy = \'offerings\'; wp