首页 > 解决方案 > 从 PHP 查询中排除标签

问题描述

我在我的子主题functions.php文件中编写了以下查询。

目前这完全符合它的需要,但是我希望扩展这个查询,以便它排除特定的标签。

  'post_type' => 'product',
  'meta_key' => 'total_sales',
  'orderby' => 'meta_value_num',
  'posts_per_page' => 10,
  'tax_query' => array(
      array(
           'taxonomy' => 'product_tag',
           'terms' => 492,
           'field' => 'id',
      ),
   ),
); 

标签: phpwordpress

解决方案


您可以使用'compare' => 'NOT IN'.

'post_type'      => 'product',
'meta_key'       => 'total_sales',
'orderby'        => 'meta_value_num',
'posts_per_page' => 10,
'tax_query' => array(
    array(
       'taxonomy' => 'product_tag',
       'terms'    => array( 492, 422 ),
       'field'    => 'id',
       'compare'  => 'NOT IN'
    ),
),

有用的链接


推荐阅读