Multiple taxonomies and meta query with url parameters
-
Hi,
I’m working on a new website for one of my client and I’m trying to do something which happens to be more difficult than I expected. I’ll try to do my best to explain what I’ve done and what I want to do.I’ve got a list of “people” (custom post type) working on some radios, that I want to display, and I want to let the user order and filter the results by meta and tax query. He can choose the following taxonomies thanks to 3 dropdown lists : radio, show, column (all 3 are taxonomies). In addition, he can select the sex (male/female, custom field), and the order of the results.
The way I try to display the good results is by getting the url parameters and use them in the query_posts like this :
<?php <!-- Get the paramaters --> $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; if( !empty($_GET['order']) ) $order=$_GET['order']; else $order='ASC'; if( !empty($_GET['sex']) ) $sex=$_GET['sex']; if( !empty($_GET['radio']) ) $radio=$_GET['radio']; if( !empty($_GET[‘show’]) ) $show=$_GET['show']; if( !empty($_GET[‘column’]) ) $column=$_GET['column']; if( !empty($_GET['name]) ) $name=$_GET['name]; <!-- If at least one paramater exists --> if (($sex && $radio || $show || $column) || ($radio || $show || $column)) { query_posts(array( 'post_type' => 'people', 'orderby' => 'title', 'order' => $order, 'posts_per_page' => 25, 'paged' => $paged, 'meta_query' => array( array( 'key' => 'sex', 'value' => $sex, 'compare' => '==' ) ), 'tax_query' => array( array( 'taxonomy' => 'radio', 'field' => 'slug', 'terms' => $radio, ), array( 'taxonomy' => 'show', 'field' => 'slug', 'terms' => $show, ), array( 'taxonomy' => 'column', 'field' => 'slug', 'terms' => $column, ), ) )); } else { query_posts(array( 'post_type' => 'people', 'orderby' => 'title', 'order' => $order, 'name' => $name, 'posts_per_page' => 25, 'paged' => $paged, 'meta_query' => array( array( 'key' => 'sex', 'value' => $sex, 'compare' => '==' ) ) )); }; if ( have_posts() ) : ?>
But here is my problem : if all the parameters are filled, the query works. But if the user only choose the sex, or the « radio », or even multiple parameters but not all, the query does not work. I have to make it work in all situations, and with every paramaters possible, whether the user selected them or not.
For example : display people that are male, and working on VirginRadio, in the Morning Show. Or only people that work on VirginRadio. Or only females in every radios…
Do someone know how I could do that ?
Thanks 😉
- The topic ‘Multiple taxonomies and meta query with url parameters’ is closed to new replies.