WordPress.org

Ready to get started?Download WordPress

Forums

Multiple taxonomies and meta query with url parameters (3 posts)

  1. abiwab
    Member
    Posted 6 months ago #

    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 ;)

  2. essaysnark
    Member
    Posted 6 months ago #

    Didn't dig too deeply into your code, but shouldn't it be

    if (($sex || $radio || $show || $column) || ($radio || $show || $column)) {

    (that's || instead of && between the first two variables)

  3. abiwab
    Member
    Posted 6 months ago #

    Even in that situation it does not work. For example if the user only selects the $radio, it won't display any results :/

Reply

You must log in to post.

About this Topic