args for WP_query with 2 different custom taxonomies (2 posts)

  1. somtam
    Posted 3 years ago #

    Hello everybody,

    I have this scenario: one custom post type and two custom taxonomies.
    I'm inside the archive page of my custom post type. I pass the id of taxonomy 1 with $_get variable.
    I would like to get:

    - a list of post, group by taxonomies 2, where taxonomy 1 = $_get variable

    but I don't know how to change the args of WP_query :(
    Right now I arrived here, but as you can see, I don't know where to insert the taxonomy 2

    $args = array(
       'post_type' => 'custom_post_type_slug',
       'tax_query' => array(
          'taxonomy' => 'taxonomy_01_name',
          'field' => 'id',
          'terms' => my_get_variable,

    and the result should be:

    - post
    - post
    - post
    - post
    - post
    - post


    thanks a lot!!!!

  2. vtxyzzy
    Posted 3 years ago #

    You must use filters to sort on a taxonomy name. The filters must do the following:

    • Join the wp_terms, wp_term_taxonomy, and wp_term_relationships tables to select the taxonomy.
    • Add the term name field to the field list.
    • Set the 'ORDER BY' clause.

    Here is some sample code that should get you started:

    // Use filters to sort a query on terms of a selected taxonomy.
       // See http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/
       // for the code of the filters used.
       $taxonomy = 'madeof';  // The taxonomy slug
       $mam_global_join = "JOIN $wpdb->term_relationships tr ON ($wpdb->posts.ID = tr.object_id)
       JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = '$taxonomy')
       JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
       $mam_global_fields = ", t.name AS term_name";
       $mam_global_orderby = "term_name ASC, $wpdb->posts.post_title ASC";
       $args = array(
          'ignore_sticky_posts' => 1,
          'post_type' => 'post',
       $q = new WP_Query($args);
       $mam_global_join = $mam_global_fields = $mam_global_orderby = ''; // Turn off filters

    Of course, you need the filter functions defined here: http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/

    And you need to substitute your own taxonomy slug and $args, and probably change $mam_global_orderby.

Topic Closed

This topic has been closed to new replies.

About this Topic