• Hi,

    I’m currently building a site which requires me to query the posts for terms in multiple taxonomies.

    I have a post type called ‘projects’ which has two taxonomies registered to it ‘artists’, and ‘visibility’

    I need to query the projects for an artist term and a visibility term. My query currently looks like this:

    $args = array(
    	'post_type' => array('projects'),
    	'artists_tax' => $artist,
            'visibility_tax' => 'active',
    	'showpost' => 1
    );

    But, it doesn’t work. WP seems to just ignore the second taxonomy.

    Has anyone come across this and is there a fix?

    I’ve tried installing the ‘Query Multiple Taxonomies’ plugin but it doesn’t seem to do the trick. Maybe I’m missing something?

Viewing 8 replies - 1 through 8 (of 8 total)
  • <?php
    //simulate a "taxonomy__in" query for 3.0
    $taxonomy = "'genre'";
    $term_ids = "('3', '13')";
    $post_status = "'publish'";
    $post_type = "('post', 'page', 'book', 'project')" ;
    
    $query = "SELECT $wpdb->posts.* FROM $wpdb->posts
    INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE 1=1
    AND $wpdb->term_taxonomy.taxonomy = $taxonomy
    AND $wpdb->term_taxonomy.term_id IN $term_ids
    AND $wpdb->posts.post_type IN $post_type
    AND $wpdb->posts.post_status = $post_status
    GROUP BY $wpdb->posts.ID
    ORDER BY $wpdb->posts.post_date DESC";
    
    $my_posts=$wpdb->get_results($query);
    
    if ($my_posts) {
      foreach($my_posts as $post) {
        setup_postdata($post);
        ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
      <?php }
    }
    ?>
    Thread Starter tbp278

    (@tbp278)

    Thanks!

    Should this accept:

    $taxonomy = "('artists_tax', 'visibility_tax')";

    I need to query one term from one taxonomy, and one term from the other taxonomy. This seems to return nothing if I enter more than one taxonomy.

    …Or am I being slow?

    Thread Starter tbp278

    (@tbp278)

    Surely there has to be a way to achieve this…

    My SQL knowledge isn’t that strong ?? Serves me right for growing up on WP queries I guess.

    Didn’t test this but you would also need to change this

    AND $wpdb->term_taxonomy.taxonomy = $taxonomy

    to

    AND $wpdb->term_taxonomy.taxonomy IN $taxonomy

    Thread Starter tbp278

    (@tbp278)

    @michaelh You are truly a wonderful individual. An SQL god amongst mere mortals. Thank you!

    Thread Starter tbp278

    (@tbp278)

    Ah – slight problem:

    This selects terms with ids of 3 OR 13. I need 3 AND 13.

    You’re already my favourite person of the day…don’t let me down now ??

    Thread Starter tbp278

    (@tbp278)

    AND ($wpdb->term_taxonomy.term_id = '3' AND wpdb->term_taxonomy.term_id = '13')

    doesn’t work ??

    There’s a slight error in your above code (missing $ for var and wrong table).

    It should be.

    AND ($wpdb->terms.term_id = 3 AND $wpdb->terms.term_id = 13)

    NOTE: You don’t need quotes for numeric values in SQL queries.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Query multiple custom taxonomies’ is closed to new replies.