繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM