Need help with multiple taxonomy query
-
Hello. I am stuck on trying to use GET variables to inform a query modifying function I’ve written to retrieve custom post type posts that have two terms associated with them, each one being from a different custom taxonomy. Being quite novice, I’ve based this function off of code I’m using for a great search function I found on this site, which works well.
Here is what my function looks like (placed in functions.php) :
function two_tax_query($query) { if(is_tax('destination_county')) { $query->set('post_type', 'destinations'); $county_slug = $_GET['destination_county']; $dest_type = $_GET['dest_type']; // create arrays because that is required by terms arg $county_slugs = array($county_slug); $dest_types = array($dest_type); $tax_query_args = array( array( 'taxonomy' => 'destination_county', 'field' => 'slug', 'terms' => $county_slugs, 'operator' => 'AND' ), array( 'taxonomy' => 'destination_category', 'field' => 'slug', 'terms' => $dest_types, 'operator' => 'AND' ) ); $query->set('tax_query', $tax_query_args); } } add_action('pre_get_posts', 'two_tax_query', 1001);
The above function is run when WP encounters a query for the “destionation_county” custom tax type. It is started with a link from a menu I created by listing the counties terms and building the link in such a way as to include an extra GET variable. This extra GET variable is the term from the other tax type I would like to include into the query. Here’s what the links look like:
<a href="https://www.thewebsite.org/?destination_county=adams-county&dest_type=cultural" title="Adams County">Adams County</a>
Sadly, this doesn’t want to work. By that I mean no errors are thrown, but my site breaks in such a way that it seems every call to wp_nav_menu does not run.
Please let me know if this is going about it the wrong way. I would prefer not to use a plugin.
- The topic ‘Need help with multiple taxonomy query’ is closed to new replies.