【问题标题】:WordPress Custom Taxonomies WP_QueryWordPress 自定义分类法 WP_Query
【发布时间】:2013-09-29 01:07:30
【问题描述】:

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

  1. 当前特价
  2. meineke-差异
  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;

?>

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    您可以试试这个(tax - 使用分类 slug。自 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 );
    

    【讨论】:

    • 给了我一个空白屏幕。我完全复制了你的 $args 数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2012-12-31
    • 2017-12-16
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多