按自定义分类查询帖子,按POST_MODIFIED排序

时间:2011-09-22 作者:Vitaliy

我试图查询cpt“空缺”中的5个职位。查询非常复杂。首先,我需要属于ID为18的自定义分类法并按“post\\u modified”排序的帖子,然后-cpt“空缺”中的所有其他帖子也按“post\\u modified”排序。所有这些都限制在5个职位上。

目前我使用:

$query_hot = $wpdb->get_results("(SELECT * FROM wp_posts
WHERE post_type = \'vacancy\' AND post_status = \'publish\'
AND ID IN (SELECT object_id FROM wp_term_relationships WHERE
wp_term_relationships.term_taxonomy_id = 18)
ORDER BY post_modified DESC)
UNION
(SELECT * FROM wp_posts
WHERE post_type = \'vacancy\' AND post_status = \'publish\'
AND ID NOT IN (SELECT object_id FROM wp_term_relationships WHERE
wp_term_relationships.term_taxonomy_id = 18)
ORDER BY post_modified DESC) LIMIT 5");
它查询正确的帖子,但不按“post_modified”排序。我做错了什么?

1 个回复
SO网友:Jono Warren

这个WP_Query 该函数可用于自定义分类法和post类型,可能比直接查询数据库更简单。以下内容应满足您的需求:

<?php $query_hot = new WP_Query( array(
    \'post_type\' => \'vacancy\',
    \'field\' => \'18\', // You can use the ID or slug here
    \'orberyby\' => \'modified\',
    \'posts_per_page\' => 5
)); 
while($query_hot->have_posts()) : $query_hot->the_post(); ?>

     <!-- Whatever you want to show in your loop -->

<?php endwhile; wp_reset_postdata(); // reset the query ?>
请参阅Codex文章WP_Query 更多信息。

结束