WordPress.org

Ready to get started?Download WordPress

Forums

List category posts
Problem with custom taxonomies (3 posts)

  1. frisco
    Member
    Posted 2 years ago #

    Following up on this post on custom taxonomies, is there an error in line 61 where taxonomy is set to 'topic-tag'?

    Wsherliker, the original author, had:

    // Added custom taxonomy support
    if ($this->params['taxonomy'] != "" && $this->params['tags'] != "") {
    $args['tax_query'] = array(array(
    'taxonomy' => 'topic-tag',
    'field' => 'slug',
    'terms' => explode(",",$this->params['tags'])
    ));
    } elseif ($this->params['tags'] != "") {
    $args['tag'] = $this->params['tags'];
    }

    But shouldn't that be:

    // Added custom taxonomy support
            if ($this->params['taxonomy'] != "" && $this->params['tags'] != "") {
              $args['tax_query'] = array(array(
                  'taxonomy' => $this->params['taxonomy'],
                  'field' => 'slug',
                  'terms' => explode(",",$this->params['tags'])
              ));
            } elseif ($this->params['tags'] != "") {
              $args['tag'] = $this->params['tags'];
            }

    so that it gets the taxonomy as a parameter?

    For me, the original code didn't work to grab a custom taxonomy, but my modified code worked fine.

    Also, can multiple custom taxonomies be supported? This could be done with the relation setting of tax_query, but I couldn't find where this was implemented. Any plans to add?

    http://wordpress.org/extend/plugins/list-category-posts/

  2. fernandobt
    Member
    Plugin Author

    Posted 2 years ago #

    Hey frisco, thanks for posting this. I'm rolling out a bug fix release: 0.19.3 with this code included.

    Regarding multiple custom taxonomies, I'll look into that, see if I can add it in a future release.

    Thanks!

  3. Kat Hagan
    Member
    Posted 2 years ago #

    Thanks for this! I was overjoyed to dig into the code and find that custom taxonomies are supported.

    I had to make a small modification so that it would use BOTH the category and the custom taxonomy option (returning results that were in either the post's category or the provided taxonomy). Adding multiple custom taxonomies would be pretty much the same.

    Here's what I did (in CatList.php):

    // Added custom taxonomy support
            if ( !empty($this->params['taxonomy']) && !empty($this->params['tags']) ) {
              $args['tax_query'] = array(
                  'relation' => 'OR',
                  array(
                    'taxonomy' => $this->params['taxonomy'],
                    'field' => 'slug',
                    'terms' => explode(",",$this->params['tags'])
                  ),
                  array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => explode(",",$this->lcp_category_id)
                  )
              );
            } else if ( !empty($this->params['tags']) ) {
              $args['cat'] = $this->lcp_category_id;
              $args['tag'] = $this->params['tags'];
            }
            else {
              $args['cat'] = $this->lcp_category_id;
            }

    The "relation => OR" is what does the magic of combining the results of the two arrays. You'd use "AND" if you want to return only posts that are in both.

    Also, you have to take the $args['cat'] out at the top of that function (I just declared $args as an empty array); if 'cat' is defined in $args, it will ignore the tax_query and JUST return results from the category, so that's why the ugly "else" code up there.

    Just a five-minute hack but maybe it will help someone.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic