Querying two taxonomies

I’ve got two taxonomies: “type of guide”, and “tools”.

They look something like this:

Read More

Type of guide

- Article
- Video
- Cheat sheet

Tool

— Learning Management System

  • Quiz Tool

    - Hide a grades column (article)
    - Creating a Gradebook (article)
    - Long Answer Question Steps (article)
    - Matching Questions (article)
    - True or False (article)
    
    - Using the Quiz Tool (video)
    
    - Using the Quiz Tool (cheat sheet)
    
  • Notifications tool

  • Grades Tool

The screenshot outlines the problem perfectly: I need to limit that tax-query to include only items that are tagged with “Quiz Tool”.

Screen: http://cl.ly/0t3J143q0A1X2w2y2X2n

I’m trying to do it with a tax_query, with an AND relation:

$args = array(
        'tax_query' => array(
            'relation' => 'AND', 
            array(
                'taxonomy' => 'guide-type',
                'field' => 'slug',
                'terms' => 'video'
            ),
            array(
                'taxonomy' => 'tool',
                'field' => 'slug',
                'terms' => what do I put here to get the current tool (Quiz tool)? 
                )
        )
    );
    $query = new WP_Query( $args );

But I just can’t figure out how to limit to only list items that are tagged with “Quiz Tool”. Note the, “what do I put here” part.

Help appreciated.

UPDATE:

It just occurred to me that I don’t have to query both taxonomies, instead exit the tax_query and use a ‘get_the_category()’ as part of the $args:

// The Query
    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'guide-type',
                'field' => 'slug',
                'terms' => 'self-paced '
            )
        ), 
        'category_name' => get_the_category()

Something like that? Any ideas how I could limit that ‘category_name’ to be the current one I’m looking at (Quiz Tool).

Terry

Related posts

Leave a Reply

3 comments

  1. This helper function will return the ID of the term you are currently viewing (from any taxonomy – you can limit just to a specific taxonomy, or the built-in tag and category taxonomies).

    It will return false if you are not on a taxonomy term archive page.

    function wpse52578_get_current_term_id(){
        if( !is_tax() && !is_tag() && !is_category() )
           return false;
    
        //If we got this far we are on a taxonomy-term page
        // (or a tag-term or category-term page)
        $taxonomy = get_query_var( 'taxonomy' );
        $queried_object = get_queried_object();
        $term_id =  (int) $queried_object->term_id;
    
        return $term_id;
    }
    

    Then you can do something like the following:

    $tool_term_id = wpse52578_get_current_term_id();
    $tax_query = array( 
                 'relation' => 'AND', 
                 array(
                    'taxonomy' => 'guide-type',
                    'field' => 'slug',
                    'terms' => 'video'
                ));
    
    if( $tool_term_id ){
         $tax_query[] =  array(
                    'taxonomy' => 'tool',
                    'field' => 'id',
                    'terms' => $tool_term_id
                    )
     }
    
    $args = array('tax_query' => $tax_query);
    $query = new WP_Query( $args );
    

    Note: I’ve not tested this

  2. This should get all post data for the taxonomy “Tool” with term “Quiz Tool”.

    $posts = get_content_taxonomy('tool', 'quiz-tool');
    
    function get_content_taxonomy($tax, $section){
        $wp_query = new WP_Query();
        $wp_query->query(array('posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'tax_query' => array(array('taxonomy' => $tax, 'field' => 'slug', 'terms' => $section))));
        $posts = $wp_query->posts;
        if(!empty($posts)){
            $return = $posts;
        }
        return $return;
    }
    
  3. Alternate way to do this is to use get_the_terms() to figure out what tax term page you’re on, then pass that to the tax_query.

    // Get an array of current terms, use the last one returned     
    
     $terms = get_the_terms( $post->ID, 'tool' );
    
        if ( $terms && ! is_wp_error( $terms ) ) { 
            $current_tax_terms = array();
    
            foreach ( $terms as $term ) {
                $current_tax_terms[] = $term->name;
            }
    
            //Use end to give me just the last term             
            $current_term = end($current_tax_terms);
        }
    

    Now I can pass $current_term into my tax_query:

    $args = array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'guide-type',
                    'field' => 'slug',
                    'terms' => 'self-paced'
                ),
                array(
                    'taxonomy' => 'tool',
                    'field' => 'slug',
                    'terms' => $current_term
                )
            ), 
            'author_name' => 'saltcod'
    
        );
        $query = new WP_Query( $args );
    
    
        $the_query = new WP_Query( $args );
    

    I’m returning the last item in the array, because that’s tax page I’m on. You could return the value from any position in the array you wanted. I happened to want the last one and so used end(array).