I’m trying to use query_posts to get posts that both belong to a category, and posts that share a TAG of the same category name.
I want to pull up all posts from the the “Food” category, as well as all posts with the tag “food” (or “Food” if case is an issue).
The object here, is to pull posts that are NOT in the food category, but are related to food. I’d also like to do this dynamically, which means I’d like to check to see if the tag of a post is the same as the category I’m searching for (this is for a category archive).
I can’t figure out the syntax for the query, anyone know how to achieve this?
Is this for the main (category) query or do you want to do a secondary query that gets the posts with tags that have the same name (“food”) as the current category?
To get the current category id on a category template (archive.php, category.php) you can use this:
$current_cat = get_query_var('cat'); // category ID $current_cat_slug = get_query_var('category_name'); // category slug (only on category page)
I know how to retrieve the current category, and slugs, I just don’t know how to either:
Combine two query searches, then sort the results (ASC)
Do one query search that returns all posts from a certain category, AND all posts that contain a tag that matches the category string. (that’s an OR statement though, posts don’t need to be in the category AND have the tag, they simply need either or).
I don’t program much in PHP, but if I was to do this in another language, I would logically do one query, save the results as a variable, do another query, save those results, combine the results (array), then sort the array – That is, assuming you can’t use an OR statement in a query_posts call.
[ Please do not bump, that’s not permitted here. ]
- The topic ‘query_posts Category and Tag of same name.’ is closed to new replies.