WordPress / 4 MIN READ
Querying Multiple Categories (and Tags) in Urls
Querying Multiple Categories (and Tags) in Urls
From the original Fervor library. Examples may use older package versions.
๐น Tutorial: Querying Multiple Categories (and Tags) in WordPress URLs
WordPress supports URL parameters (query vars) that let you filter posts by category, tag, or both. Hereโs how you can use them.
1. Query by a Single Category
Use the category slug in the URL:
https://example.com/?category_name=news
๐ This shows all posts in the news category.
2. Query by a Single Tag
Use the tag slug in the URL:
https://example.com/?tag=update
๐ This shows all posts tagged update.
3. Query by Category AND Tag
Combine them:
https://example.com/?category_name=news&tag=update
๐ Posts must be in the news category and have the update tag.
4. Query by Multiple Categories
The cat query var uses category IDs (not slugs). Separate IDs with commas:
https://example.com/?cat=2,6
๐ Shows posts in category 2 OR 6.
To exclude a category, add a minus sign:
https://example.com/?cat=2,6,-4
๐ Posts in categories 2 OR 6, but NOT category 4.
5. Query by Multiple Categories AND a Tag
You can combine cat with tag:
https://example.com/?cat=2,6&tag=update
๐ Posts in category 2 OR 6, with the tag update.
6. Pretty Permalink Style (Optional)
If permalinks are enabled and your theme supports it, you can use a cleaner URL:
https://example.com/category/news+events/
๐ Shows posts in the news OR events categories.
โ ๏ธ Not always supported โ requires theme rewrite rules.
7. Forcing AND Conditions (Advanced)
By default, cat=2,6 means โcategory 2 OR category 6โ.
If you want โcategory 2 AND category 6โ, youโll need a custom query via code (using tax_query in WP_Query).
Example in PHP:
$query = new WP_Query([
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'news',
],
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'events',
],
],
]);
๐ This will only return posts that are in both categories.
โ Quick Reference Table
| What You Want | Example URL |
|---|---|
| One category | ?category_name=news |
| One tag | ?tag=update |
| Category + tag | ?category_name=news&tag=update |
| Multiple categories (IDs) | ?cat=2,6 |
| Multiple categories + tag | ?cat=2,6&tag=update |
| Exclude category | ?cat=2,6,-4 |
๐น Bonus: Use Category Slugs Instead of IDs
By default, cat= only works with category IDs. If you prefer to use slugs (like news or events), you can add a custom function in your themeโs functions.php.
Step 1. Add This Code to functions.php
/**
* Allow multiple category slugs via ?catslug=
* Example: ?catslug=news,events
*/
function query_by_multiple_category_slugs( $query ) {
if ( ! is_admin() && $query->is_main_query() && isset($_GET['catslug']) ) {
$slugs = explode(',', sanitize_text_field($_GET['catslug']));
$query->set('category_name', implode(',', $slugs));
}
}
add_action('pre_get_posts', 'query_by_multiple_category_slugs');
Step 2. Use It in the URL
Now you can query posts by multiple slugs instead of IDs:
https://example.com/?catslug=news,events
๐ This will return posts in the news OR events categories.
You can even combine it with a tag:
https://example.com/?catslug=news,events&tag=update
๐ Posts in news or events and tagged update.
Step 3. Optional โ Excluding Categories
If you want to exclude slugs, you can extend the code by adding a minus sign check:
if ( ! is_admin() && $query->is_main_query() && isset($_GET['catslug']) ) {
$slugs = explode(',', sanitize_text_field($_GET['catslug']));
$tax_query = [];
foreach ($slugs as $slug) {
if (substr($slug, 0, 1) === '-') {
$tax_query[] = [
'taxonomy' => 'category',
'field' => 'slug',
'terms' => ltrim($slug, '-'),
'operator' => 'NOT IN',
];
} else {
$tax_query[] = [
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $slug,
];
}
}
$query->set('tax_query', $tax_query);
}
Now you can do:
https://example.com/?catslug=news,events,-sports
๐ Posts in news or events, but not sports.
โก Why This Is Useful
- You donโt have to look up numeric category IDs anymore.
- You can send clean links to clients or teammates.
- Works with any theme without hacking core WP queries.