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

Mike Schinkel mikeschinkel at newclarity.net
Tue Apr 14 04:34:36 GMT 2009


Simon:

Though I'm not sure exactly what you are looking for, I *think* that what you want is to use "category__in"

$args = array(
	'post_type' => $post_type,
	'what_to_show' => 'posts',
	'post_status' => 'publish',
	'posts_per_page' => $num_posts,
	'orderby' => 'modified',
	'order' => 'DESC',
	'category__in' => explode(",",$cat_ids),  // Assumes $cat_ids is a comma separated string of category ids.
);
$new_wp_query = new WP_Query( $args );

If this isn't what you need, reply with more details and we'll see what we can do to help.

-Mike Schinkel
Custom Wordpress Plugins
http://mikeschinkel.com/custom-wordpress-plugins

----- Original Message -----
From: "Simon Wheatley" <simon at sweetinteraction.com>
To: wp-hackers at lists.automattic.com
Sent: Monday, April 13, 2009 5:40:34 PM GMT -05:00 US/Canada Eastern
Subject: Re: [wp-hackers] Setting up a WP Query with a search

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