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

本文标签: search filter add priority to posttype and add order to some posttype