Support » Plugin: List category posts » List only posts that belong to two or more categories (Solution)

  • Resolved Henrique Vianna


    First of all, thanks picandocodigo for the great plugin!

    With a few modifications I achieved a way to list posts that belong to two or more categories. Here’s my solution:

    In the shortcode, use + instead of the comma to separate category IDs or names, like this:

    [catlist id=3+4] or [catlist name=sports+baseball] will list only posts that belong to both categories.

    You’ll need to make the following modifications in the CatList.php file, inside the include folder.

    IMPORTANT – the changes proposed below are for the version 0.28 of the plugin. Make a backup copy of the original files!

    Locate this: (line 29 in the original file)

    $args = array('cat'=> $this->lcp_category_id);

    And change to:

    if (is_array($this->lcp_category_id))
      $args = array('category__and' => $this->lcp_category_id);
      $args = array('cat'=> $this->lcp_category_id);

    Replace the function get_lcp_category() (lines 113-137 in the original file) for this:

    private function get_lcp_category(){
        if ( isset($this->params['categorypage']) &&
          $this->params['categorypage'] == 'yes' ):
          $this->lcp_category_id = $this->lcp_get_current_category();
        elseif ( !empty($this->params['name']) ):
          if (preg_match('/\+/', $this->params['name'])):
            $categories = array();
            $cat_array = explode("+", $this->params['name']);
            foreach ($cat_array as $category) :
              $id = $this->get_category_id_by_name($category);
              $categories[] = $id;
            $this->lcp_category_id = $categories;
          elseif (preg_match('/,/', $this->params['name'])):
            $categories = '';
            $cat_array = explode(",", $this->params['name']);
            foreach ($cat_array as $category) :
              $id = $this->get_category_id_by_name($category);
              $categories .= $id . ",";
            $this->lcp_category_id = $categories;
            $this->lcp_category_id = $this->get_category_id_by_name($this->params['name']);
        elseif ( isset($this->params['id']) && $this->params['id'] != '0' ):
          if (preg_match('/\+/', $this->params['id']))
            $this->lcp_category_id = explode("+", $this->params['id']);
            $this->lcp_category_id = $this->params['id'];

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Fernando Briano


    Awesome, thanks! I’m adding some stuff to release a 0.29 version soon, so I’ll check this out.


    Plugin Author Fernando Briano


    hvianna this has been added to version 0.29, again thank you very much! I see you have a user with the same handle on GitHub, check the plugin’s repo over there and contribute as much code as you want 😀

    GitHub’s Pull Requests are awesome, and you get more credit for the code you contribute 🙂


    Nice, glad I could help. And thank you again for the helpful plugin!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘List only posts that belong to two or more categories (Solution)’ is closed to new replies.