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

时间:2017-11-14 15:07:11

标签: php wordpress woocommerce

我想只显示两个类别,一个来自wp类别(类别名称 - php),一个来自woo类别(类别名称 - 服装),并隐藏剩余的woo和wp类别,除了php和衣服。我怎么能这样做?

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' );

2 个答案:

答案 0 :(得分:0)

$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 );

答案 1 :(得分:0)

最后我得到了解决方案的人......

           `$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'
                )
            );`