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 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.

Read more here:: search filter add priority to post_type and add order to some post_type

Leave a Reply

Your email address will not be published. Required fields are marked *