• I wanted different sidebars based on term-filtered list – rather than just on the taxonomy itself. So I added the following code:

    public function get_sidebars($handle = "!= '2'") {
    		global $wpdb, $post_type, $wp_query;
    [ ... ]
    
    		if(is_singular()) {
    
    [ ... ]
    		// Taxonomy archives
    		} elseif(is_tax() || is_category() || is_tag()) {
    
    			$term = get_queried_object();
    
    			$terms = array();
    			foreach ($wp_query->tax_query->queries as $taxterm) {
            foreach ($taxterm['terms'] as $k => $termid) {
              $termobj = get_term_by( $taxterm['field'], $termid, $taxterm['taxonomy'] );
    					$terms[] = $termobj->slug;
            }
          }
    
    			$joins .= "LEFT JOIN $wpdb->term_relationships term ON term.object_id = posts.ID ";
    			$joins .= "LEFT JOIN $wpdb->term_taxonomy taxonomy ON taxonomy.term_taxonomy_id = term.term_taxonomy_id ";
    			$joins .= "LEFT JOIN $wpdb->terms terms ON terms.term_id = taxonomy.term_id ";
    			$joins .= "LEFT JOIN $wpdb->postmeta post_tax ON post_tax.post_id = posts.ID AND post_tax.meta_key = 'taxonomies'";
    
    			$where .= "(terms.slug IN('".implode("','",$terms)."')";

    It might not work 100% for post tag archives – but it’s running on a site that doesn’t use posts, so that’s not a problem for me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Joachim Jensen

    (@intoxstudio)

    Taxonomy terms archives should work. What post type and taxonomies have you had trouble with?

    get_queried_object() – on the taxonomy archive pages – contains an object with the following attributes: (sample data included)

    [term_id] => 6
    [name] => Cat 2
    [slug] => cat-2
    [term_group] => 0
    [term_taxonomy_id] => 6
    [taxonomy] => category
    [description] =>
    [parent] => 0
    [count] => 2
    [cat_ID] => 6
    [category_count] => 2
    [category_description] =>
    [cat_name] => Cat 2
    [category_nicename] => cat-2
    [category_parent] => 0

    So obviously you can match both the current term and the taxonomy of the current term with the respective columns in the database. I am aware that the Tax Query contains the same attributes among others.

    I do not really see the point in using the IN statement, since the archive page you are looking at represents a single term (and taxonomy). Or have I misunderstood you completely?

    In my tests the sidebars appearing in an archive are the same with your code and the original code, but as you use get_term_by() your code requires another query making the plugin slower.

    Thread Starter marfarma

    (@marfarma)

    I’m using taxonomy archives with advanced taxonomy queries for listings of posts with taxonomy term1 OR term2.

    When I created a sidebar with my taxonomy and term1 selected it replaced the host sidebar on any taxonomy term page, not just for term1. Perhaps I inadvertently specified that the sidebar should show for taxonomy OR taxonomy->term1 condition? I expected it would be an AND condition.

    My thought was that archive pages didn’t support custom taxonomy term level filter.

    I really want to show a sidebar when the WP_Tax_Query Object includes a query that matches this one — when two specific terms are included in an or condition.

    [queries] => Array
                  (
                      [0] => Array
                          (
                              [taxonomy] => mytaxonomy
                              [terms] => Array
                                  (
                                      [0] => term1
                                      [1] => term2
                                  )
    
                              [include_children] => 1
                              [field] => slug
                              [operator] => IN
                          )
                  )

    The problem is that the sidebar custom post type doesn’t allow for inputs that would support it. Any suggestion?

    Plugin Author Joachim Jensen

    (@intoxstudio)

    When you select both a taxonomy and a term of the same taxonomy for your sidebar, that term will always be overruled of the taxonomy criteria.

    So to answer your question: Most of the criteria are independent of each other and therefor an OR-condition is used. I just find that most logical; e.g. either a rule is taxonomy-wide OR taxonomy-term-wide.

    Try to unpick the taxonomy and see if that shouldn’t solve the problem. However, if you have altered the query in the archive, the plugin might not work out of the box.

    Can you try to print the content of get_queried_object() here?

    One solution from my side could be to implement some hooks for custom queries, but at this moment I cannot see how this could be implemented and still be robust enough for all sorts of queries.

    Thread Starter marfarma

    (@marfarma)

    OK – we have the following code

    echo '<pre>';
          print_r(get_queried_object());
          echo '<br />';
          print_r($wp_query->tax_query);
          echo '</pre>';

    producing the following output

    stdClass Object
    (
        [term_id] => 11
        [name] => News
        [slug] => news
        [term_group] => 0
        [term_order] => 0
        [term_taxonomy_id] => 12
        [taxonomy] => mytaxonomy
        [description] =>
        [parent] => 0
        [count] => 4
    )
    
    WP_Tax_Query Object
    (
        [queries] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => mytaxonomy
                        [terms] => Array
                            (
                                [0] => news
                                [1] => event
                            )
    
                        [include_children] => 1
                        [field] => slug
                        [operator] => IN
                    )
    
                [1] => Array
                    (
                        [taxonomy] => category
                        [terms] => Array
                            (
                                [0] => 1
                            )
    
                        [include_children] =>
                        [field] => term_id
                        [operator] => NOT IN
                    )
    
            )
    
        [relation] => AND
    )

    As you can see, get_queried_object() only reports the first term – and would entirely miss any second taxonomy as well.

    I don’t believe that taxonomy terms are unique across taxonomy types. That’s why I expected an AND condition. I can imagine someone creating several rating taxonomies, each with the same terms.

    Without specifying both the taxonomy name and the term, how could you differentiate between excellent service and excellent feature-set ratings?

    Plugin Author Joachim Jensen

    (@intoxstudio)

    I will think about using the Tax Query in future releases, but I cannot promise anything. For the ordinary user it would be a bit overkill and I try to keep the plugin as light as possible.

    Taxonomy terms are indeed unique by their slugs. Not by their names though. If you create a category with the slug term1 and thereafter a tag with the slug term1, the slug of the latter will be renamed to term1-2.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Added support for filter on taxonomy archive’ is closed to new replies.