Skip to main content
Fixed typo, added an example of a URL that has multiple terms
Source Link
rmorse
  • 123
  • 9

Most of the above examples work but if you are using multiple categories NONE (as of writing, WP version 3.6) of the other methods work to get all the categories that have been passed to either "cat" or "category_name".

This is the kind of archive URL with multiple categories: https://yoursite.com/cateogry/blue+yellow/ (yeah this kind of URL works every wordpress site but its not used much)

The only way I have found is to pull the data from:

$wp_query->query['category_name']

For some reason this returns a different value to get_query_var( 'category_name' ) which only returns the first category.

When using multiple categories you will have to use some function like explode to get an array of category slugs, then loop through that to grab all the IDs:

<?php
global $wp_query;

//grab all categories from query string (if using `category_name`)
$category_slugs_array = explode("+",esc_attr($wp_query->query['category_name']));

$categories = array();
$category_ids = array();

//loop through all the slugs
foreach($category_slugs_array as $category_slug)
{
    //get category object using slug
    $category = get_category_by_slug( $category_slug );

    //check to make sure a matching category has been found
    if(isset($category->cat_ID))
    {
        $categories[] = $category;
        $category_ids[] = $category->cat_ID;
    }
}

var_dump($categories); //array of categories
var_dump($category_ids); //array of category IDs

?>

Obviously there needs to be some considerations when using AND (+) or OR (,) operators.

Most of the above examples work but if you are using multiple categories NONE (as of writing, WP version 3.6) of the other methods work to get all the categories that have been passed to either "cat" or "category_name".

The only way I have found is to pull the data from:

$wp_query->query['category_name']

For some reason this returns a different value to get_query_var( 'category_name' ) which only returns the first category.

When using multiple categories you will have to use some function like explode to get an array of category slugs, then loop through that to grab all the IDs:

<?php
global $wp_query;

//grab all categories from query string (if using `category_name`)
$category_slugs_array = explode("+",esc_attr($wp_query->query['category_name']));

$categories = array();
$category_ids = array();

//loop through all the slugs
foreach($category_slugs_array as $category_slug)
{
    //get category object using slug
    $category = get_category_by_slug( $category_slug );

    //check to make sure a matching category has been found
    if(isset($category->cat_ID))
    {
        $categories[] = $category;
        $category_ids[] = $category->cat_ID;
    }
}

var_dump($categories); //array of categories
var_dump($category_ids); //array of category IDs

?>

Obviously there needs to be some considerations when using AND (+) or OR (,) operators.

Most of the above examples work but if you are using multiple categories NONE (as of writing, WP version 3.6) of the other methods work to get all the categories that have been passed to either "cat" or "category_name".

This is the kind of archive URL with multiple categories: https://yoursite.com/cateogry/blue+yellow/ (yeah this kind of URL works every wordpress site but its not used much)

The only way I have found is to pull the data from:

$wp_query->query['category_name']

For some reason this returns a different value to get_query_var( 'category_name' ) which only returns the first category.

When using multiple categories you will have to use some function like explode to get an array of category slugs, then loop through that to grab all the IDs:

<?php
global $wp_query;

//grab all categories from query string (if using `category_name`)
$category_slugs_array = explode("+",esc_attr($wp_query->query['category_name']));

$categories = array();
$category_ids = array();

//loop through all the slugs
foreach($category_slugs_array as $category_slug)
{
    //get category object using slug
    $category = get_category_by_slug( $category_slug );

    //check to make sure a matching category has been found
    if(isset($category->cat_ID))
    {
        $categories[] = $category;
        $category_ids[] = $category->cat_ID;
    }
}

var_dump($categories); //array of categories
var_dump($category_ids); //array of category IDs

?>

Obviously there needs to be some considerations when using AND (+) or OR (,) operators.

Source Link
rmorse
  • 123
  • 9

Most of the above examples work but if you are using multiple categories NONE (as of writing, WP version 3.6) of the other methods work to get all the categories that have been passed to either "cat" or "category_name".

The only way I have found is to pull the data from:

$wp_query->query['category_name']

For some reason this returns a different value to get_query_var( 'category_name' ) which only returns the first category.

When using multiple categories you will have to use some function like explode to get an array of category slugs, then loop through that to grab all the IDs:

<?php
global $wp_query;

//grab all categories from query string (if using `category_name`)
$category_slugs_array = explode("+",esc_attr($wp_query->query['category_name']));

$categories = array();
$category_ids = array();

//loop through all the slugs
foreach($category_slugs_array as $category_slug)
{
    //get category object using slug
    $category = get_category_by_slug( $category_slug );

    //check to make sure a matching category has been found
    if(isset($category->cat_ID))
    {
        $categories[] = $category;
        $category_ids[] = $category->cat_ID;
    }
}

var_dump($categories); //array of categories
var_dump($category_ids); //array of category IDs

?>

Obviously there needs to be some considerations when using AND (+) or OR (,) operators.