Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got two taxonomies: "type of guide", and "tools".

They look something like this:

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

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer
    
Thanks for having a look at this, Stephen. $term_id doesn't seem to be getting returned. I get nothing when I echo it in my template file. ($taxonomy echos fine, but that's probably expected.) –  saltcod May 18 '12 at 15:55

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).

share|improve this answer

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;
}
share|improve this answer
    
Thanks Michael. How would I make this generic—ie: not code 'quiz-tool' into get_content_taxonomy(). I want to use this same WP_Query for all of the pages. Ideally, get_content_taxonomy (the_current_tool_i'm_in) would limit it just to the tool I'm looking at now. –  saltcod May 18 '12 at 14:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.