选择具有两个和两个相关分类的查询

时间:2011-08-31 作者:adolfozen

对于2个分类法和2个术语,这可能吗

 $querystr = "
 SELECT *
 FROM $wpdb->posts
 LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
 LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
 LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
 WHERE $wpdb->posts.post_type = \'shows\'
 AND $wpdb->posts.post_status = \'publish\'
 AND $wpdb->term_taxonomy.taxonomy = \'location\'
 AND $wpdb->term_taxonomy.taxonomy = \'genre\'
 AND $wpdb->terms.slug = \'california\'
 AND $wpdb->terms.slug = \'comedy\'
 ORDER BY $wpdb->posts.post_date DESC
 LIMIT 10
 ";

$pageposts = $wpdb->get_results($querystr, OBJECT);

if ($pposts):

foreach ($pposts as $post):
setup_postdata($post);

the_title();
echo \'<br\'>;
the_content();

endforeach;
endif;
我尝试将此代码放在上面,但没有显示任何内容;我想显示具有2个术语名称和2个分类法的帖子

例如:sampleposts[showlocation\\u tax][体裁税]

showlocation\\u税有条款

-加利福尼亚州洛杉矶

while GREEN\\u tax-喜剧-时尚

我想做的是显示所有帖子例如:

加州的喜剧节目帖子

我会非常感谢你的帮助

2 个回复
SO网友:mrwweb

我相信您可以使用WP\\u Query方法通过tax_query 像这样:

$my_query_args = array(
    \'post_type\' => \'shows\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'location\',
            \'field\' => \'slug\',
            \'terms\' => \'california\'
        ),
        array(
            \'taxonomy\' => \'genre\',
            \'field\' => \'slug\',
            \'terms\' => \'comedy\'
        )
    )
);

$my_query = new WP_Query( $my_query_args );

if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

    the_title();
    echo \'<br\'>;
    the_content();  

endwhile; endif; wp_reset_postdata();

SO网友:kevin

这里有一个函数,我用来查询2个自定义分类法和2个术语:

在里面functions.php

// MULTIPLE TAXONOMY QUERY ALLOWANCE
function posts_search ($post_type,$taxonomies) { // $taxonomies should be an array (\'taxonomy\'=>\'term\', \'taxonomy2\'=>\'term2\')
    foreach ($taxonomies as $key=>$value) {
        $args=array(\'post_type\'=>$post_type,\'post__in\'=>$ids,$key=>$value);
        unset($ids); $ids=array();
        foreach($posts=get_posts($args) as $post) { $ids[]=$post->ID; }
        if (empty($ids)) return false;
    }
    return $posts;
}
在一个template file:

$posts = posts_search (\'produtos\',array(\'prod-categoria\'=>$tt,\'prod-cols\'=>\'5-C-P-F-NF-P\')); 
if($posts) { 

    foreach($posts as $post) { 

        if(get_post_meta($post->ID, "codigo_value", $single = true) != "") { 

            //here you can retrieve infos; i was interested in custom fields in my case
            echo get_post_meta($post->ID, "codigo_value", $single = true); 

        }

    } 
}
我这里的两个自定义分类是prod-categoriaprod-cols 我在寻找两个术语,其中一个来自变量$tt 另一个呢5-C-P-F-NF-P.

希望这有帮助。

结束

相关推荐

List custom taxonomy terms

我正在尝试显示自定义分类法的所有术语(characters), 分类法与自定义帖子类型相关(books).我想做的是显示characters, 比如说我有两个books, 我加了三个characters 致各book, 我想显示所有六个字符。我只想得到名称,而不是链接,或者列表形式,如果我可以得到一组对象或一个更可取的数组。非常感谢。