Title: Filter/multi-taxonomy query
Last modified: July 5, 2019

---

# Filter/multi-taxonomy query

 *  Resolved [mfebrey](https://wordpress.org/support/users/mfebrey/)
 * (@mfebrey)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/)
 * Hello! I’d like to add a filter option for Job Title (taxonomy) results. Would
   like to add Location (taxonomy) option to the results. Are there any code samples,
   documentation, tutorials? I’ve looked and can’t find anything. Thank you for 
   much!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Ffilter-multi-taxonomy-query%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11704644)
 * Nothing that we have ready-made for this, since every implementation is going
   to be different. This is going to be all custom code that we don’t have involvement
   in, outside of just making sure the given taxonomy is registered and ready for
   use.
 *  Thread Starter [mfebrey](https://wordpress.org/support/users/mfebrey/)
 * (@mfebrey)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11715738)
 * Can I pay(pal) you for support? Can’t find where to DM ya, happy to delete this.
 * I have the code very close but can’t get past a hump.
 *     ```
        <?php 
   
       	$args = array(
       		    'relation' => 'AND',
           		array(
           			'taxonomy' => 'location',
           			'field'    => 'slug',
           			'terms'    => 'denver-co',
           		),
           		array(
           			'taxonomy' => 'job_titles',
           			'field'    => 'slug',
           			'terms'    => 'photographer',
           		),
       	);
   
   
       	// Custom query.
           $query = new WP_Query( $args );
   
           // Check that we have query results.
           if ( $query-> have_posts() ) {
   
               // Start looping over the query results.
               while ( $query->have_posts() ) {
   
                   $query->the_post();
   
                   echo "Success <br>";
   
               }
   
           } else {
               echo "FAIL";
           };
   
   
           // Restore original post data.
           wp_reset_postdata();
   
       ?>
       ```
   
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11716035)
 * What aspect of this is failing? Not finding posts?
 * Based on the arguments made for the WP_Query call, you’d need a post to have 
   both a location term of “denver-co” AND a job title of photographer” for it to
   match and be part of your results.
 *  Thread Starter [mfebrey](https://wordpress.org/support/users/mfebrey/)
 * (@mfebrey)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11716218)
 * Something about the $query line is breaking the code. I can’t get any response
   from anything below it. Have removed my line by line echoes for ease of reading.
   But basically no echoes below the $query declaration get triggered.
 * This is my first time messing with custom queries. Does the page need the WP 
   default query to run first before adding the new query? What I’ve posted above
   is the only code on this particular page, “filters-test.php”. Fairly certain 
   this breaks from the WP architecture and avoids the background includes.
 * Does this help troubleshoot?
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11716352)
 * There’s a space in the if statement line between the `->` and the `have_posts()`
   in your version above.
 *     ```
       if ( $query-> have_posts() ) {
       ```
   
 * Should be
 *     ```
       if ( $query->have_posts() ) {
       ```
   
 * No doesn’t need the default to make a new, secondary one.
 *  Thread Starter [mfebrey](https://wordpress.org/support/users/mfebrey/)
 * (@mfebrey)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11718979)
 * Hey! Can’t even get the basic query to work.
 * Made the code as simple as possible. This is the only code on the page:
    [http://fshneco.com/v2/wp-content/themes/divi-child/filters-test.php](http://fshneco.com/v2/wp-content/themes/divi-child/filters-test.php).
 * The code breaks at the Query declaration. Tried adding get header at the top 
   and still no love. I’ll gladly post the final filter code once I’ve (we’ve) got
   this working!
 *     ```
        <?php 
   
           $args = array(
               'posts_per_page'   => -1,
               'post_type'        => 'any',
           );
   
           echo "args is ok <br>";
   
           // Custom query.
           $query = new WP_Query( $args );
   
           echo "query is ok <br>";
   
           // Check that we have query results.
           if ( $query->have_posts() ) {
   
               // Start looping over the query results.
               while ( $query->have_posts() ) {
   
                   $query->the_post();
   
                   echo "Success <br>";
   
               }
   
           } else {
               echo "FAIL";
           };
   
           echo "end of stuff"; 
   
           // Restore original post data.
           wp_reset_postdata();
   
       ?>
       ```
   
    -  This reply was modified 6 years, 11 months ago by [mfebrey](https://wordpress.org/support/users/mfebrey/).
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11719388)
 * Not managing to recreate any errors with the code provided in the last reply.
   It found me 23 posts and iterated overall of them. Each time it gave the “success”
   message.
 * Do you have any specific errors showing up somewhere like the error logs? That’d
   provide a lot better information for what’s actually happening.
 *  Thread Starter [mfebrey](https://wordpress.org/support/users/mfebrey/)
 * (@mfebrey)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11720091)
 * No errors from the page itself but found this in the log:
 * [autoindex:error]… Cannot serve directory /home/firestyleadmin/public_html/v2/
   wp-content/uploads/2019/05/: No matching DirectoryIndex (index.html.var….
 * Could this be the result of my using the full path to the file and not a WP default
   directory structure?
 * ie
    [http://fshneco.com/v2/wp-content/themes/divi-child/filters-test.php](http://fshneco.com/v2/wp-content/themes/divi-child/filters-test.php)
 * I realize this is well outside your scope. Will pay for time.
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11720145)
 * Can you contact us at [support@pluginize.com](https://wordpress.org/support/topic/filter-multi-taxonomy-query/support@pluginize.com?output_format=md)
   and we can take some of this away from public eye.
 * Thanks.
 *  Thread Starter [mfebrey](https://wordpress.org/support/users/mfebrey/)
 * (@mfebrey)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11730747)
 * FINAL WORKING CODE
 * I hope this benefits someone else. Thanks a ton for the help Michael Beckwith!
 * I am passing query vars via GET. This code was placed in my child theme folder
   and saved as filters.php. Then created a “Search” page in WordPress and assigned
   it the “Filters” template.
 * Sample URL: [https://www.fshneco.com/v2/search/?myloc=denver-co&myjob=model](https://www.fshneco.com/v2/search/?myloc=denver-co&myjob=model)
 *     ```
       <?php 
   
       /* 
       Template Name: Filters
       */
   
       //grab variables from page url
       $loc = htmlspecialchars($_GET["myloc"]);
       $job = htmlspecialchars($_GET["myjob"]);
   
       $args = array(
   
          //query only these custom post types
          'post_type' => array('collab', 'event'),
   
          //argument specifically for taxonomy
          'tax_query' => array(
   
          //AND because both conditions need to be met
          'relation' => 'AND',
   
          //search tax terms based on GET vars from above
          array(
             'taxonomy' => 'location',
             'field'    => 'slug',
             'terms'    => $loc,
          ),
          array(
             'taxonomy' => 'job_titles',
             'field'    => 'slug',
             'terms'    => $job,
          ),
        ),
       );
   
       //the actual query
       $query = new WP_Query( $args );
   
       //loop with results
       if ( $query->have_posts() ) {
   
          while ( $query->have_posts() ) {
            $query->the_post();
            $title = get_the_title();
   
            echo "Success= ".$title." <br>";
   
         }
   
       } else {
        echo "No results found.";
       };
   
       wp_reset_postdata();
   
       ?>
       ```
   
    -  This reply was modified 6 years, 11 months ago by [mfebrey](https://wordpress.org/support/users/mfebrey/).
    -  This reply was modified 6 years, 11 months ago by [mfebrey](https://wordpress.org/support/users/mfebrey/).
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11730977)
 * Woo, working things!

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Filter/multi-taxonomy query’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-type-ui/assets/icon-256x256.png?rev=2744389)
 * [Custom Post Type UI](https://wordpress.org/plugins/custom-post-type-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-type-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-ui/reviews/)

 * 11 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [6 years, 11 months ago](https://wordpress.org/support/topic/filter-multi-taxonomy-query/#post-11730977)
 * Status: resolved