[wp-hackers] Setting up a WP Query with a search

Simon Wheatley simon at sweetinteraction.com
Mon Apr 13 21:40:34 GMT 2009


On Mon, Apr 13, 2009 at 8:21 PM, SoJ Web <sojweb at indiana.edu> wrote:
>
> Are you just trying to modify or override the default WP search? There are a
> few filters you can use to modify the query fairly extensively. The Search
> Everything plugin has some nice code that can give you an idea of working
> with that stuff:
>

Thanks for the suggestion. To (attempt to) clarify a little:

Currently I'm creating listings of recent posts in categories for use
on various templates by defining a WP_Query thus:

$args = array(
	'post_type' => $post_type,
	'what_to_show' => 'posts',
	'post_status' => 'publish',
	'posts_per_page' => $num_posts,
	'orderby' => 'modified',
	'order' => 'DESC',
	'cat' => $cat_ids,
);
$new_wp_query = new WP_Query( $args );

I was hoping for something similar, using WP_Query, which would allow
me to return posts as per a WP search but with the results restricted
to a particular set of categories. I'm fine with using the WHERE
filters on the SQL generated by the WP_Query and so I could do my SQL
LIKE stuff here, but I hoped it might be possible to reuse the
existing code in WP_Query which manages the WP search results to
generate my restricted search results. (Seems like less code
duplication.)

I thought it might be as simple as:

$args = array(
	's' => $search_string,
);
$search_wp_query = new WP_Query( $args );

...but that doesn't seem to be working for me. :(

Are you suggesting that I should kick off a WP_Query as per my first
example and then use the WHERE filters to mess with the resultant SQL?

Thanks again.

S


P.S. Search Everything is certainly a nicely coded plugin. :)


---
Sweet Interaction Ltd is Registered in England/Wales, no. 6610741
Registered office: 7 Malton Av, Manchester, M21 8AT


More information about the wp-hackers mailing list