Wordpress,带自定义分类和自定义帖子类型的WP_Query

时间:2010-04-15 15:54:35

标签: wordpress

此代码获得10个链接到term_name的所有类型的帖子;

global $wp_query;
query_posts( array(  
    "taxonomy_name" => "term_name", 
    'showposts' => 10 ) 
);

此代码获得10个自定义帖子类型“消息”的帖子;

global $wp_query;
query_posts( array(  
    'post_type' => 'message' 
    'showposts' => 10 ) 
);

但是,此代码始终忽略post_type要求,但仍选择链接到term_name的所有帖子类型;

global $wp_query;
query_posts( array(  
    'post_type' => 'message' ,
    "taxonomy_name" => "term_name",
    'showposts' => 10 ) 
);

我看不出两者是如何单独工作的,但除非它可能是一个错误,否则他们不会一起工作 - 任何想法?

3 个答案:

答案 0 :(得分:5)

看起来像个错误。你试过custom select query吗?这应该这样做:

$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 = 'message' 
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.taxonomy = 'taxonomy_name'
    AND $wpdb->terms.slug = 'term_name'
    ORDER BY $wpdb->posts.post_date DESC
    LIMIT 10
    ";

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

我在构建查询时使用了this answer

答案 1 :(得分:1)

已提交 - core.trac.wordpress.org/ticket/13020。修复只是将query.php文件的一行更改为和if语句,以检查post_type是否为空。

答案 2 :(得分:0)

使用'category_name'代替'taxonomy_name'。适用于WP 3.5.2

query_posts( array(  
    'post_type' => 'message',
    'category_name' => 'term_name',
    'showposts' => '10' ) 
);