1

I'll keep this super concise the normal search works perfectly, but when I've added some checkboxes for additional features, etc…the tax_query doesn't work however I know the $_GET is working as I've output the results at the top left of the page for testing.

Live site: http://costaricalifestyleproperties.com.s221952.gridserver.com/

if(!empty($_GET['ct_lifestyle'])) {
//if(is_array($_GET['feature'])) {
    $ct_lifestyle = $_GET['ct_lifestyle'];

    foreach ($ct_lifestyle as $lifestyle):
        $search_values['tax_query'] = array (
            'taxonomy' => 'lifestyle',
            'field' => 'slug',
            'terms' => $lifestyle,
            'operator'  => 'IN'
        );
        echo $lifestyle;
    endforeach;
//}

}

I commented out the is_array() for testing and one checkbox could be chosen instead of multiples. The custom taxonomy also works and I've double and triple check the name. Here's the register function as well below.

Any help is greatly appreciated I've been banging my head trying to get this to work for hours, I know its gotta be something simple I'm missing.

if(!function_exists('ct_lifestyle_taxonomies')) {

add_action( 'init', 'ct_lifestyle_taxonomies', 0 );

function ct_lifestyle_taxonomies() {

    // Lifestyle
    $lifestylelabels = array(
        'name' => __( 'Lifestyle', 'contempo' ),
        'singular_name' => __( 'Lifestyle', 'contempo' ),
        'search_items' =>  __( 'Search Lifestyles', 'contempo' ),
        'popular_items' => __( 'Popular Lifestyles', 'contempo' ),
        'all_items' => __( 'All Lifestyles', 'contempo' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Lifestyle', 'contempo' ),
        'update_item' => __( 'Update Lifestyle', 'contempo' ),
        'add_new_item' => __( 'Add New Lifestyle', 'contempo' ),
        'new_item_name' => __( 'New Lifestyle Name', 'contempo' ),
        'separate_items_with_commas' => __( 'Separate Lifestyles with commas', 'contempo' ),
        'add_or_remove_items' => __( 'Add or remove Lifestyles', 'contempo' ),
        'choose_from_most_used' => __( 'Choose from the most used Lifestyles', 'contempo' )
    );
    register_taxonomy( 'lifestyle', 'listings', array(
        'hierarchical' => false,
        'labels' => $lifestylelabels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'lifestyle' ),
    ));

    if(!function_exists('lifestyle')) {
        function lifestyle() {
            global $post;
            global $wp_query;
            $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'lifestyle', '', ', ', '' ) );
            echo esc_html($terms_as_text);
        }
    }

}

}

  • If you look at $your_query_object->request after the query is run, you'll see the SQL that was sent to the database. $your_query_object will also contain all of the query vars including the parsed tax queries, so you can see exactly what's going wrong. – Milo Oct 19 '17 at 1:21
  • How would I output that or do I need to check the server logs? – contempoinc Oct 19 '17 at 1:34
1

Here:

$search_values['tax_query'] = array (

You're resetting the value of tax_query for every item in the foreach loop.

It's also being set to an invalid value:

Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays). This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy arrays.

So to build up a tax_query of multiple arrays, you need to set $search_values['tax_query'] to an empty array, then append each array:

if( ! empty( $_GET['ct_lifestyle'] ) ) {
    $ct_lifestyle = (array) $_GET['ct_lifestyle'];

    $search_values['tax_query'] = array();

    foreach ($ct_lifestyle as $lifestyle):
        $search_values['tax_query'][] = array (
            'taxonomy' => 'lifestyle',
            'field'    => 'slug',
            'terms'    => $lifestyle,
        );
    endforeach;
}

Also note this bit:

$ct_lifestyle = (array) $_GET['ct_lifestyle'];

Casting it as an array means that if only one ct_lifestyle parameter is present then it will get put into an array with a single item. That way your foreach will still work without you having to handle a single item differently.

That code has the assumption that you want the query to match posts that have all the given 'lifestyes'. This is why I removed the IN operator from your code. If you want to match posts that have any of the given lifestyles you can skip the foreach and just pass the array into the one tax_query:

if( ! empty( $_GET['ct_lifestyle'] ) ) {
    $ct_lifestyle = (array) $_GET['ct_lifestyle'];

    $search_values['tax_query'] = array(
        array(
            'taxonomy' => 'lifestyle',
            'field'    => 'slug',
            'terms'    => $ct_lifestyle,
            'operator' => 'IN',
        )
    );
}
  • Thanks for the help man, much appreciated! I tried both and it's still returning all results and ignoring the checked boxes. Each listing can have one lifestyle or multiple tagged. – contempoinc Oct 19 '17 at 1:42
  • Where does $search_values come from, and what are you doing with it after setting these values? Is all this happening inside pre_get_posts, or a custom query? – Jacob Peattie Oct 19 '17 at 1:45
  • Its a custom query here's the entire search-listings.php the lifestyle tax_query is located between lines 50 & 63 gist.github.com/contempoinc/b9ed47a2a52b22c9e2c1f1f93a0a966b – contempoinc Oct 19 '17 at 1:51
  • Ok, there's a lot going on there. But there's a few things that jumped out at me. Firstly, your meta queries have the same problem. meta_query and tax_query are arrays of arrays, but most of the times you set them you're setting them to a single array. You're also resetting tax_query at least once, under /* Exclude Ghost Status */. That's overwriting anything you'd done to tax query before. You need to go through your code and make sure that you're accounting for the possibility that these values are already set and adding to them, not resetting. – Jacob Peattie Oct 19 '17 at 2:03
  • That was it, you're the man!!!! I just ripped out the tax_query for Ghost Status and I'll have to rework the rest but its working for now, thanks a ton you're an absolute ROCKSTAR!!!! – contempoinc Oct 19 '17 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.