Search code examples
checkboxtagstaxonomywordpress

Wordpress relation taxonomy posts query not giving desired results


I've created a filter using all checkboxes to display particular posts by tag. I want the results of the posts returned to contain all the tags based on the checkbox. I can set the tax_query to relation => 'AND' but it returns nothing if it does not get all the variables. The user should be allowed to check as little or as many tag filters as they would like but the resulting posts should have all the tags. Here is the function that does the processing..

function CaseStudiesAjaxFunction() {
global $post;

$advertiser = isset( $_GET["advertiser"] )? $_GET["advertiser"] : '';
$agency = isset( $_GET["agency"] )? $_GET["agency"] : '';
$automotive = isset( $_GET["automotive"] )? $_GET["automotive"] : '';
$education = isset( $_GET["education"] )? $_GET["education"] : '';
$financial = isset( $_GET["financial"] )? $_GET["financial"] : '';
$retail = isset( $_GET["retail"] )? $_GET["retail"] : '';
$travel = isset( $_GET["travel"] )? $_GET["travel"] : '';
$search = isset( $_GET["search"] )? $_GET["search"] : '';
$social = isset( $_GET["social"] )? $_GET["social"] : '';
$smartpath = isset( $_GET["smartpath"] )? $_GET["smartpath"] : '';
$halogen = isset( $_GET["halogen"] )? $_GET["halogen"] : '';
$kenshoo = isset( $_GET["kenshoo"] )? $_GET["kenshoo"] : '';
$techvalidate = isset( $_GET["techvalidate"] )? $_GET["techvalidate"] : '';
$infinityawards = isset( $_GET["infinityawards"] )? $_GET["infinityawards"] : '';

$args = array(
            'post_type' => 'post',
            'posts_per_page' => 12,
            'tax_query' => array(
                        'relation' => 'OR',
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $advertiser
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $agency
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $automotive
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $education
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $financial
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $retail
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $travel
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $search
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $social
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $smartpath
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $halogen
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $kenshoo
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $techvalidate
                                ),
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => $infinityawards
                                )
                        ),
                        array(
                        'relation' => 'AND',
                            array(
                                    'taxonomy' => 'post_tag',
                                    'field' => 'slug',
                                    'terms' => 'case-studies'
                                )
                        )

        );
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
    echo '<div class="case-study">';
        echo '<div class="case-study-img-container">';
            the_post_thumbnail();
        echo '</div>';
        the_title(); echo '<br>';
        the_tags();
    echo '</div>';
endwhile; endif;
die();

}


Solution

  • You're using tax_query incorrectly.

    My suggestion would be to combine all of these tags into a comma delimited string and then use the tag paramater.

    http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

    Put your tags into an array to make them easier to handle. Instead of setting the value to a blank string if not set use false instead so they'll be removed by array_filter().

    http://php.net/manual/en/function.array-filter.php

    Implode the elements into a string and then set the tag parameter.

    $tags = array(
        'advertiser' => isset( $_GET["advertiser"] ) ? $_GET["advertiser"] : false,
        'agency'     => isset( $_GET["agency"] ) ? $_GET["agency"] : false,
    );
    
    $tag_string = implode( ',', array_filter( $tags ) );
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 12
    );
    
    if ( ! empty( $tag_string ) ) {
        $args['tag'] = $tag_string;
    }