I found this answer that helps with ordering query results by the post type in my search query. But the issue is that it helps when I go to
https://example.com/?s=query%20string
that is if I search using native php search. And that's great. But I also have the search via ajax - when I type, the ajax will call my callback function that is registered on a custom rest endpoint.
So I am wondering if there is any way that I can identify that custom query so that I can reuse that filter? In the conditional:
if ( ! is_admin() && is_search() && is_my_custom_search_query() ) {
The alternative is to try to rewrite the CASE
orderby to WP_Query
which I am not sure is possible tbh.
Search query for ajax looks like:
$query_string = sanitize_text_field( wp_unslash( $_GET['searchString'] ) );
$page_no = intval( $_GET['pageNo'] );
$posts_per_page = 9;
$offset = ( $page_no - 1 ) * $posts_per_page;
$search_args = array(
'posts_per_page' => $posts_per_page,
'post_status' => 'publish',
'perm' => 'readable',
's' => $query_string,
'offset' => $offset,
);
$search_results = new WP_Query( $search_args );
posts_orderby
filter, which will expose the query. I will try to add a custom parameter to theWP_Query
, and using that as an identifier.