WordPress.org

Ready to get started?Download WordPress

Forums

Show Posts From Multiple Categories (8 posts)

  1. Gemfruit
    Member
    Posted 9 months ago #

    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?

  2. vtxyzzy
    Member
    Posted 9 months ago #

    Please check your other post on this topic.

  3. Aahan Krish
    Member
    Posted 7 months ago #

    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.

  4. Gemfruit
    Member
    Posted 7 months ago #

    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.

  5. Aahan Krish
    Member
    Posted 7 months ago #

    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?

  6. Gemfruit
    Member
    Posted 7 months ago #

    Everything except for the period after the function in:

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

    Is that period a typo?

  7. Aahan Krish
    Member
    Posted 7 months ago #

    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.

  8. Gemfruit
    Member
    Posted 4 months ago #

    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.

Reply

You must log in to post.

About this Topic