我正在使用这里描述的技术Seperating Custom Post Search Results 还有这里posts_groupby problem 将我的搜索结果分开并按帖子类型分组。这意味着我有以下代码。
In functions.php
add_filter(\'posts_orderby\', \'group_by_post_type\', 10, 2);
function group_by_post_type($orderby, $query) {
global $wpdb;
if ($query->is_search) {
return $wpdb->posts . \'.post_type DESC\';
}
// provide a default fallback return if the above condition is not true
return $orderby;
}
In search.php
<?php $last_type="";
$typecount = 0;
while (have_posts()){
the_post();
if ($last_type != $post->post_type){
$typecount = $typecount + 1;
if ($typecount > 1){
echo \'</ol>\'; //close type container
}
// save the post type.
$last_type = $post->post_type;
//open type container
switch ($post->post_type) {
case \'post\':
echo "<h2>News Articles</h2><ol>";
break;
case \'page\':
echo "<h2>Pages</h2><ol>";
break;
case \'work\':
echo "<h2>Projects</h2><ol>";
break;
case \'bootlegs\':
echo "<h2>Bootlegs</h2><ol>";
break;
case \'directors\':
echo "<h2>Directors</h2><ol>";
break;
//add as many as you need.
}
} ?>
除了我无法控制自定义帖子类型的显示顺序之外,这一切都非常有效。我猜它们是通过内部ID号订购的,但我需要控制它们。我怎样才能做到这一点?
谢谢