• Resolved progonkpa

    (@progonkpa)


    Is there a simple way for users to display all the posts of multiple categories?

    I tried this URL but it didn´t work.
    localhost/?cat=1&cat=2

    Does WP have this functionality built-in?

    • This topic was modified 2 years, 6 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 9 replies - 1 through 9 (of 9 total)
  • Is there a simple way for users to display all the posts of multiple categories?

    @progonkpa can u explain this in a bit more detail. It would be really helpful. Thanks 🙂

    Thread Starter progonkpa

    (@progonkpa)

    @soberbanda sure.

    When you click a category, you see all the posts from the category you clicked.
    But what if you want to display the posts from 2 or more categories?
    How can one do that?

    I was hoping that WordPress provided a way to do that, that’s what I was talking about with my example localhost/?cat=1&cat=2.

    If WordPress doesn’t have a way of doing this, one can always fall back on plugins (recommendations?) or coding it.

    @progonkpa How about child categories? More on that: https://www.wpbeginner.com/glossary/category/

    I found this plugin: https://ww.wp.xz.cn/plugins/posts-from-category/

    Let me know how it goes 🙂

    In case it helps, the URL for multiple categories is formed like this: localhost/?cat=1,2.
    https://developer.ww.wp.xz.cn/reference/classes/wp_query/#category-parameters

    WordPress will probably title the archive with the first category in the URL, though.

    Thread Starter progonkpa

    (@progonkpa)

    Thanks @rickymccallum87, I wasn’t aware of this option. It works indeed as you described. It shows all posts from multiple categories while WP only displays the title of the first category.

    Adding to that, it only works in the plain permalink structure. It does not work with post name as permalink structure. At least that was what I saw when I tried.

    @soberbanda Child categories is something that would show all posts of all child categories but that isn’t my use case. One can go this route as a workaround but I will not go there as it will add unacceptable complexity imo 🙂

    Thanks guys for the input. This functionality is not urgent atm. When I need it, I’ll create it.

    @progonkpa Awesome – Best of luck 🙂

    You’re welcome. Still, it’s an interesting problem so I went a bit further with it.

    I’ve just found out that you can comma-separate slugs in the Post Name permalink structure, too: localhost/category/first-cat,second-cat/.

    To fix the archive title, you could use the get_the_archive_title filter. This function would create a more appropriate title for any multiple-category archive.

    add_filter('get_the_archive_title', 'wpsf_multiple_categories_title', 10, 3);
    function wpsf_multiple_categories_title($title, $original_title, $prefix)
    {
      global $wp_query;
      $category_name_param = $wp_query->query['category_name'];
      $category_slugs = explode(',', $category_name_param);
    
      // When looking at an archive with multiple categories...
      if (is_category() && count($category_slugs) > 1) {
    
        // Convert category slugs to title case category names
        $category_names = array_map(function ($slug) {
          return get_term_by('slug', $slug, 'category')->name;
        }, $category_slugs);
    
        // Put them in a comma-separated list
        $original_title = implode(', ', $category_names);
      }
    
      // Use that list as the archive title
      return $original_title;
    }
    Thread Starter progonkpa

    (@progonkpa)

    Wow @rickymccallum87, that’s it 🙂
    Tested and working.

    Afaik, these are all possible scenarios.
    http://localhost/?cat=cat1,cat2
    http://localhost/category/cat1,cat2/
    http://localhost/category/cat1/cat2/

    So I refactored the code to cover all scenarios.

    add_filter('get_the_archive_title', 'wpsf_multiple_categories_title', 10, 3);
    function wpsf_multiple_categories_title($title, $original_title, $prefix)
    {
        $cats_query_data = get_categories_query_data();
    
        if (!$cats_query_data['separator'])
            return $original_title;
    
        $category_slugs = explode($cats_query_data['separator'], $cats_query_data['args']);
        // Convert category slugs to title case category names
        $category_names = array_map(function ($slug) {
            return get_term_by('slug', $slug, 'category')->name;
        }, $category_slugs);
    
        return implode(', ', $category_names);
    }
    
    function get_categories_query_data(): array
    {
        if (!is_category())
            return ['separator' => 0];
    
        if (strpos($_REQUEST['cat'], ','))
            return ['separator' => ',', 'args' => $_REQUEST['cat']];
    
        global $wp_query;
        $category_name_args = $wp_query->query['category_name'];
    
        if (strpos($category_name_args, ',')) {
            return ['separator' => ',', 'args' => $category_name_args];
        } elseif (strpos($category_name_args, '/')) {
            return ['separator' => '/', 'args' => $category_name_args];
        } else {
            return ['separator' => 0];
        }
    }
    Thread Starter progonkpa

    (@progonkpa)

    This line
    if (strpos($_REQUEST['cat'], ','))

    Needs to become
    if (isset($_REQUEST['cat']) && strpos($_REQUEST['cat'], ','))

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display the posts of multiple categories’ is closed to new replies.