如何在wordpress php中一次显示Woo Category(服装)和WP Category(php)



我只想显示两个类别,一个来自wp类别(类别名称 - php(,一个来自woo类别(类别名称 - 服装(,并隐藏除php和服装之外的其余woo和wp类别。我该怎么做?

function custom_pre_get_posts_query( $q ) {
  $tax_query = (array) $q->get( 'tax_query' );
  $tax_query[] = array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'clothing' ), // clothing is woo category name
       'operator' => 'IN'
  );
  $tax_query[] = array(
       'taxonomy' => 'category',
       'field' => 'slug',
       'terms' => array( 'php' ), // php is wp category name
       'operator' => 'IN'
  );
  $q->set( 'tax_query', $tax_query );
}
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
$args = array(
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array('clothing'),
        'operator' => 'IN'
    ),
    array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array('php'),
        'operator' => 'IN'
    )
  )
);
$query = new WP_Query( $args );

最后我得到了解决方案伙计们...

           `$tax_query[] = array(
                'relation' => 'OR',
                array(
                    'taxonomy' => 'category',
                    'field' => 'term_id',
                    'terms' => array('php'),
                    'operator' => 'IN'
                ),
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => array('clothing'),
                    'operator' => 'IN'
                )
            );`

最新更新