fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
โ† WordPress

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.


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.

Keep your curiosity going.Explore more WordPress โ†’
287 TUTORIALS ยท 22 TOPICSREADY