admin管理员组文章数量:1418047
I don't want to change the core SEARCH function. I have default search function and only updating search.php
file in the theme. I added search_filter function into functions.php.
function search_filter($query)
{
if (!is_admin() && $query->is_main_query()) {
if ($query->is_search) {
$query->set('post_type', array('page', 'post', 'fsgallery'));
$query->set('order', array('post_date' => 'DESC'));
}
}
}
add_action('pre_get_posts', 'search_filter');
I have 3 post_type in my Wordpress and I want to show all page result first and then combine POST and GALLERY results with ordered by date. Right now order by DESC result shows everything by ordered and could not show page first.
What I need is: I want to show all page result first. After that I want to show post and gallery ordered by date.
I don't want to change the core SEARCH function. I have default search function and only updating search.php
file in the theme. I added search_filter function into functions.php.
function search_filter($query)
{
if (!is_admin() && $query->is_main_query()) {
if ($query->is_search) {
$query->set('post_type', array('page', 'post', 'fsgallery'));
$query->set('order', array('post_date' => 'DESC'));
}
}
}
add_action('pre_get_posts', 'search_filter');
I have 3 post_type in my Wordpress and I want to show all page result first and then combine POST and GALLERY results with ordered by date. Right now order by DESC result shows everything by ordered and could not show page first.
What I need is: I want to show all page result first. After that I want to show post and gallery ordered by date.
Share Improve this question edited Aug 9, 2019 at 10:40 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Aug 8, 2019 at 19:07 BayanaaBayanaa 331 silver badge6 bronze badges 3- Look here – nmr Commented Aug 9, 2019 at 10:52
- I already tried this one. First its not combining post and fsgallery post_types. I need to combine them and order by Desc. Also it breaks core search. I mean when i search some words the result shows many wrong pages. – Bayanaa Commented Aug 9, 2019 at 20:10
- ooh wow. WP search is finding from image alt tag and it shows many pages. It was my fault. But its still not combining post and gallery :P.Otherwise all close to fix – Bayanaa Commented Aug 9, 2019 at 20:32
1 Answer
Reset to default 0Here is the code from the provided link after adaptation.
add_filter( 'posts_orderby', 'se344727_custom_search_order', 10, 2 );
function se344727_custom_search_order( $orderby, $query )
{
global $wpdb;
if (!is_admin() && $query->is_main_query())
{
if ($query->is_search)
{
// -- sort by: type, title, date --
// " ELSE 2 END ASC, " . $orderby;
// -- sort by: type, date --
// " ELSE 2 END ASC, {$wpdb->prefix}posts.post_date DESC;
$orderby =
" CASE WHEN {$wpdb->prefix}posts.post_type = 'page' THEN 1 " .
" ELSE 2 END ASC, " .
$orderby;
}
}
return $orderby;
}
本文标签: search filter add priority to posttype and add order to some posttype
版权声明:本文标题:search filter add priority to post_type and add order to some post_type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745252830a2649925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论