[wp-hackers] Modify Search Functionality

Bill Smith ml_wordpress at copperleaf.org
Tue Jun 21 19:26:45 GMT 2005

Nikolay Bachiyski wrote:

> ml_wordpress at copperleaf.org wrote:
> > I want to be able to add some columns in a table I've added with a
> > plugin to the standard search. How would I go about doing this? Is 
> there
> > any kind of filter that I can use to modify the query string?
> >
> > Bill
> Let us talk in code first:
> function modify_search_string($query) {
>     $query->set('s', 'baba');
> }
> add_action('parse_query', 'modify_search_string');
> $query is a reference to the WP_Query object whose parse_query is 
> called. For details about what can you do with $query see the WP_Query 
> code in classes.php (nope, I didn't find WP_Query docs in the codex).

Let me go into some more detail here. I have an additional table that 
has the post id as one of the columns and and another column that is a 
text column.
The query I want to generate would be something like

SELECT DISTINCT wp_post.* FROM wp_post
LEFT JOIN mytable ON wp_post.ID = mytable.ID
WHERE ((wp_post.post_title LIKE '%word%')
   OR (wp_post.post_content LIKE '%word%')
   OR (mytable.description LIKE '%word%'))
AND (post_status = "publish")
ORDER BY  post_date DESC LIMIT 0, 10

Where it gets nasty for me is I need to 1) add the left join. That 
doesn't seem too bad since there is the posts_join filter. However 
adding the additional or piece for mytable seems to be a challenge. Here 
is the "where" string that I get passed in the posts_where filter. With 
multiple words in the search list, it seems to get even nastier. I would 
need to insert my piece in there inside the parens.

AND   0=1    AND (((post_title LIKE '%word%') OR (post_content LIKE 
'%word%')) OR (post_title LIKE '%word%') OR (post_content LIKE 
'%word%')) AND post_date_gmt <= '2005-06-21 19:12:59' AND (post_status = 

Is there a cleaner way than this?


More information about the wp-hackers mailing list