对于WordPress网站,我有几个自定义帖子类型,名为cities
和books
, 还有定期的博客帖子。我自定义了搜索结果(仅在帖子标题中搜索)页面,以便按自定义帖子类型对结果进行排序。
问题是,自定义帖子类型中的搜索结果并没有真正分组。
Example:
当我搜索信件时am
, 我在搜索结果页面中获得以下结果:
BOOKS
-----
Book 1
CITIES
------
City 1
BOOKS
-----
Book 2
Book 3
CITIES
------
City 2
City 3
City 4
BLOGPOSTS
---------
Post 1
Post 2
Post 3
etc.
我想要什么:
BOOKS
-----
Book 1
Book 2
Book 3
CITIES
------
City 1
City 2
City 3
City 4
BLOGPOSTS
---------
Post 1
Post 2
Post 3
etc.
这是我的循环:
<?php if(have_posts()) : ?>
<?php
$last_type = "";
$typecount = 0;
while ( have_posts() ) : the_post();
?>
<?php
if ($last_type != $post->post_type){
$typecount = $typecount + 1;
if ($typecount > 1){
echo \'</div><!-- close type-container -->\'; //close type container
}
// save the post type.
$last_type = $post->post_type;
//open type container
switch ($post->post_type) {
case \'books\':
echo "<div class=\\"books-container\\"><h2>";
_e(\'Books\', \'qode\');
echo "</h2>";
break;
case \'cities\':
echo "<div class=\\"cities-container\\"><h2>";
_e(\'Destinations\', \'qode\');
echo "</h2>";
break;
case \'post\':
echo "<div class=\\"blog-container\\"><h2>";
_e(\'Blogposts\', \'qode\');
echo "</h2>";
break;
}
}
?>
<?php if(\'books\' == get_post_type()) : ?>
<?php get_template_part(\'templates/books_search\', \'loop\'); ?>
<?php elseif(\'cities\' == get_post_type()) : ?>
<?php get_template_part(\'templates/cities_search\', \'loop\'); ?>
<?php elseif(\'post\' == get_post_type()) : ?>
<?php get_template_part(\'templates/blog_search\', \'loop\'); ?>
<?php endif; ?>
<?php endwhile; // endwhile have_posts ?>
<?php // Pagination ?>
<?php if($qode_options_proya[\'pagination\'] != "0") : ?>
<?php pagination($wp_query->max_num_pages, $blog_page_range, $paged); ?>
<?php endif; ?>
<?php else: //If no posts are present ?>
<div class="entry nothing-found">
<h3><?php _e(\'Sorry, there is no 100% \', \'qode\') . " " . the_search_query() . " " . _e(\' yet.\') ?></h3>
<p><?php _e(\'But there are loads of other 100% destinations available. Try again in the searchfield below.\', \'qode\'); ?>
</div>
<?php endif; // endif have_posts ?>
这是包含的CPT模板文件的一个示例:
<div <?php post_class(\'vc_span4 wpb_column column_container\'); ?> style="padding-bottom: 60px;">
<div class="wpb_wrapper book">
<?php // Get the post thumbnail ?>
<div class="wpb_single_image wpb_content_element element_from_fade element_from_fade_on">
<div class="wpb_wrapper">
<?php
if ( has_post_thumbnail() ) {
echo \'<a href="\' . get_permalink( $thumbnail->ID ) . \'" title="\' . the_title_attribute( \'echo=0\' ) . \'" class="book-holder" ><span class="book-thumb">\';
echo get_the_post_thumbnail( $post->ID, \'book-thumbnail\' );
echo \'</span></a>\';
}
?>
</div>
</div>
</div>
<div class="wpb_text_column wpb_content_element text-align-center" style="margin: 20px 0px 0px 0px; ">
<div class="wpb_wrapper">
<h5><?php the_title(); ?></h5>
</div>
<a href="<?php the_permalink(); ?>" target="_blank" class="qbutton small" style="margin: 20px 0px 0px 0px; "><?php _e(\'More Info\', \'qode\'); ?></a>
</div>
</div>
我一定是做错了什么,但我不知道是什么。
有人能告诉我哪里出了问题吗?
最合适的回答,由SO网友:Pieter Goosen 整理而成
默认情况下,此类型的订购不可用。但是,这里有两个基本选项
示例
(
这将进入您的函数。php)
add_filter( \'the_posts\', function( $posts, $q )
{
if( $q->is_main_query() // Targets the main query only
&& $q->is_search() // Only targets the search page
) {
usort( $posts, function( $a, $b )
{
$post_types = array ( // Set your post type order here
\'books\' => 1,
\'cities\' => 2,
\'post\' => 3
);
$posts = $post_types[$a->post_type] - $post_types[$b->post_type];
if ($posts === 0) {
//same post_type, compare by post_date.
return $a->post_date < $b->post_date;
}else{
//different post_type, save to ignore the post_date.
return $posts;
}
} );
}
return $posts;
},
10, 2 );