views:

10

answers:

1

I am working on a wordpress theme and need to select posts that are in multiple categories to build a menu. I could set it up to work off of the tags, or a combination of tags and categories, but in the end I need to be able to search by multiple cat/tag. The only built-in wordpress function that I have been able to find that allows for a multiple AND search on cat/tag is the query_posts function as such:

query_posts(array('category__and' => array(list_of_cats)))

There are two problems with that. The first is that the wp docs say not to use it. The second is a real problem in that I need to know how many posts were queried before running my loop since I don't want to build submenus that are empty or only have one or two items in them.

The alternative is to run my own query but I can't quite figure out the right statement to run to find posts in the selected categories. I can visualize what needs to happen to search for two categories, but it gets fuzzy at 3+.

I can execute multiple, independent queries and then use array_intersect to find the those that match all queries but I would like to expand my mysql and/or wp knowledge a bit here.

In summary, I would appreciate an answer to any one of the following questions:

1 - an alternative to query_posts() that allows for multiple AND matching
2 - a way to find the number of posts that will be looped when using query_posts()
3 - some mysql magic that runs a big union on multiple queries to the same table

Thanks in advance

A: 

"If you want to create separate Loops outside of the main one, you should use get_posts() instead.

Note: Beginning with Version 3.0, an array of Page ID also can be used for the include and exclude parameters."

Pretty sure you can do something like:

$wp_query->post_count

To get the total number of queried posts.

Yeah, according to WPQuery:

get_posts() Fetch and return the requested posts from the database. Also populate $posts and $post_count.

:)

Kory
Thanks, I couldn't find that post_count variable... I wrote a custom loop that queries for each individual category and then finds the overlap... This will be much cleaner code. Thanks again
baiano
Of course. Glad that I could be of help. Thanks for accepting!
Kory