search filter add priority to post_type and add order to some post_type

I don't want to change the core SEARCH function. I have default search function and only updating search.php file i

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
Add a comment  | 

1 Answer 1

Reset to default 0

Here 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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252831a4618780.html

相关推荐

  • search filter add priority to post_type and add order to some post_type

    I don't want to change the core SEARCH function. I have default search function and only updating search.php file i

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信