• I’m trying to figure out the syntax to display posts from multiple categories on a single category page. I’ve done this with query_posts, but I’ve since switched to using pre_get_posts, and can’t figure out the syntax. I’ve got a function in my functions.php file that checks to see if I’m on the desired page, which works, but I can’t figure out the actual query part to grab posts from multiple categories, by name.

    Here’s some code I’ve tried:

    This code seems to only care about 1 of the categories listed, not both. It also doesn’t take an array, which is desired, as I declare the types for use in earlier code, and would love to avoid manually typing them again.

    $query->set('category_name', 'random', 'featured'); //can't take array

    This is another way to access the category names, but also didn’t yield the results I was looking for.

    $query->query_vars['category_name'] = 'random'; //also doesn't take an array

    So my question is, how do I display posts from multiple categories and use pre_get_posts?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Please check your other post on this topic.

    Had the same issue today, and here’s how it should be done:

    $query->set( 'category_name', 'random, featured' );

    That’s right. As you can see here, the category parameter category_name, takes only a string. So you need to put the category slugs in one string.

    And if you are using a variable, here’s how it’d be done:

    $query->set( "category_name", "{$get_cat_dynamically}, featured" );

    NOTE: The double quotes are important in the second case.

    Thread Starter Gemfruit

    (@gemfruit)

    So for your first example, basically I was doing ‘cat1’, ‘cat2’, where as I needed one string that would be parsed, such as ‘cat1, cat2’. Did I get that right?

    As for the second dynamic example, the syntax is confusing to me, even with some Java / C# background. What exactly is going on here?

    “{$get_cat_dynamically}, featured” );

    It looks like a function passed as a parameter, as well as a manual category added to the returned string (hence the double quotes, and the lack of ending the string after the function). I also assume $get_cat_dynamically is a custom, example function, and not a built in function. If you could clear up my assumptions, that would be great ??

    Thanks a lot for all the info, very helpful stuff.

    So for your first example, basically I was doing ‘cat1’, ‘cat2’, where as I needed one string that would be parsed, such as ‘cat1, cat2’. Did I get that right?

    Yes. You need to have multiple category slugs in one string, and not each in their own.

    As for the second dynamic example, the syntax is confusing to me…

    A small correction, and a better example:

    // Say... (just to show how a variable can be used; you might know this already)
    $get_cat_dynamically = 'random';
    
    $query->set( 'category_name', $get_cat_dynamically.', featured' );

    Is this better?

    Thread Starter Gemfruit

    (@gemfruit)

    Everything except for the period after the function in:

    $query->set( ‘category_name’, $get_cat_dynamically.’, featured’ );

    Is that period a typo?

    No. That’s not a typo.

    $get_cat_dynamically = 'random';
    
    // Now this:
    $query->set( 'category_name', $get_cat_dynamically.', featured' );
    // OR this:
    $query->set( "category_name", "{$get_cat_dynamically}, featured" );
    
    // Is equivalent to:
    $query->set( 'category_name', 'random'.', featured' );
    
    // Which in turn is equivalent to:
    $query->set( 'category_name', 'random, featured' );

    The period is being used to concatenate the strings.

    Thread Starter Gemfruit

    (@gemfruit)

    I recently came back to this, and still can’t get an array to work. Here’s my code:

    function bars_category( $query ) {
    
    		$categories = array ('bars', 'lodging');
    
    		if ($query->is_main_query() && is_category($categories)) {
    
    			echo("{$categories}");
    
    			$query->set("category_name", "{$categories}, music");
    			return $query;
    		}
    	}
    	add_action( 'pre_get_posts', 'bars_category' );

    ‘music’ posts show up, but nothing from the $categories array does. I tried making the $categories array a single item, such as bars, but it still doesn’t work. I know the array is functional, because the is_category() call is satisfied, so there’s nothing wrong with that.

    Simply put, an array in $query->set doesn’t seem to work, despite thinking it should.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Show Posts From Multiple Categories’ is closed to new replies.