0

How to sort search result by post_title, then by post_content For example - if i search for word "Ball" i want to get results where "ball" exists in title, then the results where "ball" exists in the content.

With the help of ChatGPT i got this selution. But this is a normal sort by title not by search results

function custom_search_query($query) {
    if ($query->is_search) {
        $query->set('orderby', 'post_title');
        $query->set('order', 'ASC');
    }
    return $query;
}
add_filter('pre_get_posts','custom_search_query');

PS: Am using this api for searching https://example.com/wp-json/wp/v2/posts?search=text

2
  • ChatGPT hasn't given you anything close to what you're asking for (surprise, surprise). You want to weight search results with a higher weight for matches in the title. WordPress already sorts search results this way. Matches in the post title are given a higher priority than matches in the content: wpsmackdown.com/wordpress-search-improvements-3-7 Commented Mar 7, 2023 at 12:45
  • For some reason this is not correct in my case. Anyway i wrote raw SQL with order by case Commented Mar 7, 2023 at 14:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.