WordPress无法获得所有尊重标签和类别条件的帖子

时间:2013-11-09 13:13:49

标签: php wordpress custom-post-type

我已经创建了一个自定义类型的帖子,我希望获得所有类别和标签中的帖子。不幸的是,我不知道为什么它只返回一个帖子(我应该至少有3个或4个)

  

这是我的代码:

$tag = $_GET["tag"];
$cat = $_GET["cat"];

$args = array(
    'post_type' => 'post_product',
    'post_status' => 'publish',
    'numberposts' => -1,
    array(
        array(
            'taxonomy' => 'cating'
        ),
        array(
            'taxonomy' => 'tagging')
    )
);

$custom_query = new WP_Query( $args );

if ( $custom_query->have_posts() ):

    while ( $custom_query->have_posts() ) :
        $custom_query->the_post();

        $product_terms = wp_get_object_terms(get_the_ID(), 'cating');
        $product_terms_tag = wp_get_object_terms(get_the_ID(), 'tagging');

        if(!empty($product_terms) && !empty($product_terms_tag)){
            if(!is_wp_error( $product_terms ) && !is_wp_error( $product_terms_tag )){

                foreach($product_terms as $term){
                    if(strcmp($term->name, $cat) == 0) {

                        foreach($product_terms_tag as $term_tag){
                            if(strcmp($term_tag->name, $tag) == 0) {

                                // display here

                            }
                        }
                    }
                }
            }
        }
    }
}

有没有人知道为什么我只收到一个帖子或者什么也没有,因为它们是尊重这些条件的帖子。

谢谢。

2 个答案:

答案 0 :(得分:0)

您的Taxonomy Query parameters状况不佳。并且您没有使用任何$_GET变量。

假设您通过$_GET获取了术语ID,它应该是这样的:

$args = array(
    'post_type' => 'post_product',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
         array(
            'taxonomy' => 'tagging',
            'field' => 'id',
            'terms' => (int) $tag
        ),
        array(
            'taxonomy' => 'cating',
            'field' => 'id',
            'terms' => (int) $cat
        )
    )
);

答案 1 :(得分:0)

将$ post_count添加到$ args数组中,并附上您想要的帖子数

相关问题