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

SoJ Web sojweb at indiana.edu
Mon Apr 13 23:23:50 GMT 2009


I think I get what you're saying: You want to have a number of mini- 
loops that list posts from certain categories that match some search  
term? Try something like this:

// query for a given term ("search_term") in some given categories (7  
and 8)
$search_query = new WP_Query();
$search_posts = $search_query->query('s=search_term&cat=7,8');

// build loop
foreach($search_posts as $search_post) {
	setup_postdata($search_post);
	
	// do stuff
}

And you can put those mini-loops wherever you need them. Is that the  
sort of thing you were looking for?

-Jeff


On Apr 13, 2009, at 5:40 PM, Simon Wheatley wrote:

> 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
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list