WordPress自定义分类法WP_Query


WordPress Custom Taxonomies WP_Query

我正在尝试创建一个查询,其中我在自定义帖子类型中创建多个类别(分类法),然后根据特定工作正常的主页查询。目前我有 3 个分类法:

  1. 当前特价
  2. 梅内克差异
  3. 特色

我已经编写了拉取这些的代码。我遇到的问题是,在主页上,它只需要在它们也附加到"特色"分类法时拉取这些帖子。因此,这方面的标准逻辑示例是:

如果分类法=当前特价和特色,那么成功否则失败

但它所做的是将它们全部拉出,因为当前代码是 OR,我需要 AND

思潮?(下面的代码)

<?php
$post_type = 'coupons';
$tax = 'coupons_category';
$tax_terms = get_terms($tax);
if ($tax_terms):
    foreach ($tax_terms as $tax_term):
        echo '<div id="'.$tax_term->slug.'" class="coupon-box '.$tax_term->slug.'">';
        $args = array(
            'post_type' => $post_type,
            "$tax" => array($tax_term->slug, 'featured'),
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts' => 1
        );
        $myQuery = null;
        $myQuery = new WP_Query($args);
        if($myQuery->have_posts()):
            while ($myQuery->have_posts()) : $myQuery->the_post();
            $price = get_field('price_image', $myQuery->ID);
            $print = get_field('print', $myQuery->ID);
            $product = get_field('product_box_image', $myQuery->ID);
            $title = get_the_title();
            $content = get_the_content();
            echo '<div class="fourty9 left box center">';
                echo '<h1>'.$title.'</h1>';
                echo '<p class="center"><img src="'.$price.'" /></p>';
                echo '<p>'.$content.'</p>';
                echo '<p class="center"><a href="'.$print.'">Print Coupon</a></p>';
                echo '<p class="center"><img src="'.$product.'" alt="Filter"></p>';
            echo '</div>';  

            endwhile;
        endif;
        echo '</div>';
        wp_reset_query();
    endforeach;
endif;
?>
你可以

试试这个(tax - 使用分类学蛞蝓。从版本 3.1 开始不推荐使用"tax_query")

$args = array(
    'post_type' => 'coupons',
    'posts_per_page' => -1,
    'caller_get_posts' => 1,
    'tax_query' => array(
        array(
            'taxonomy' => 'coupons_category',
            'field' => 'slug',
            'terms' => array( 'current-specials', 'featured' ),
            'operator' => 'AND'
        )
    )
);
$query = new WP_Query( $args );