1

The native search page runs a query like so:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* 
FROM wp_posts 
WHERE 1=1 AND (((wp_posts.post_title LIKE '%keyword%') 
OR (wp_posts.post_content LIKE '%keyword%')) 
AND (wp_posts.post_password = '') 
AND wp_posts.post_type IN ('post', 'page', 'attachment') 
AND (wp_posts.post_status = 'publish') 
ORDER BY wp_posts.post_date DESC LIMIT 0, 10

I am passing specific post IDs to the query by hooking into pre_get_posts so I don't need the other WHERE conditions related to the keyword. e.g.

add_filter('pre_get_posts','my_search_filter');
function my_search_filter($query) {
    if ($query->is_search) {
            // post IDs within distance X
        $matches = array(1,3,5);
        $query->query_vars['post__in'] = $matches;

    }
}

How can I remove the conditions related to post_title and post_content from the query object by modifying the existing query rather than writing a new one.

  • Why are you using a search if you don't want to actually search anything...? Could you give a brief outline of what you are trying to achieve - there is probably a better than modifying a search query :D – Stephen Harris Feb 24 '12 at 10:28
  • I'm geocoding the address the user enters in the search form to build a list of matching post IDs within a certain distance which I store in $matches. – kidrobot Feb 24 '12 at 22:00
2

Filter posts_where or posts_search. See this answer for a couple of examples.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.