WP主题开发WordPress带自定义分类和自定义帖子类型的WP_Query

此代码获得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 ) 
);

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

看起来像个bug.你试过 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.

原文链接:,转发请注明来源落伍老站长!

发表评论