Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a new query for a custom post type of 'courses'. Those results are then to be filtered using tax_query, querying 3 custom taxonomies matching 1 or several term ID's. These are passed from a search page.

Here's the code working find up to now:

// Lets emulate the posted ID's from the course search widget
// with some static term ID's for each custom taxonomy.
$search_course_area = array(65, 62);
$search_course_level = array(117); //52 for further filtering
$search_course_mode = array(54, 56); 


//Begin a new query
$query = new WP_Query(
    array(
        //Retreive ALL course posts
        'post_type' => 'course',
        'posts_per_page' => '-1',
        //Filter taxonomies by id's passed from course finder widget
        'tax_query' => array(
                //Set the relation condition for the filters in 
                //this case we're using AND as it is explicity set 
                //by the user when searching
                'relation' => 'AND',
                //First check retreive course-area's based on ID
                array(
                    'taxonomy' => 'course-area',
                    'field' => 'id',
                    'terms' => $search_course_area
                ),
                //And again for the second taxonomy
                array(
                    'taxonomy' => 'study-levels',
                    'field' => 'id',
                    'terms' => $search_course_level
                ),
                //Finally check retreive course-level's based on ID
                array(
                    'taxonomy' => 'course-mode',
                    'field' => 'id',
                    'terms' => $search_course_mode
                ),
        )
    )
);

The thing I'm a little stuck on, is if an array is passed which is empty, this would obviously break the query and return no results.

What would be the cleanest way to tackle this? I could do something like:

if (isset($search_course_area)) {
    echo  "array(
                    'taxonomy' => 'course-area',
                    'field' => 'id',
                    'terms' => $search_course_area
                ),";
};

But I have a feeling that wouldn't be the best way to approach it?

Thanks a lot for your time and any help you can give I really appreciate it!

Craig

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You can define the args outside of the WP_Query instantiation:

<?php
$tax_query = array('relation' => 'AND');
    if (isset($search_course_area))
    {
        $tax_query[] =  array(
                'taxonomy' => 'course-area',
                'field' => 'id',
                'terms' => $search_course_area
            );
    }
    if (isset($search_course_level))
    {
        $tax_query[] =  array(
                'taxonomy' => 'study-levels',
                'field' => 'id',
                'terms' => $search_course_level
            );
    }
    if (isset($search_course_mode))
    {
        $tax_query[] =  array(
                'taxonomy' => 'course-mode',
                'field' => 'id',
                'terms' => $search_course_mode
            );
    }

$query = new WP_Query(
    array(
        //Retreive ALL course posts
        'post_type' => 'course',
        'posts_per_page' => '-1',
        //Filter taxonomies by id's passed from course finder widget
        'tax_query' => $tax_query,
    )
);
?>
share|improve this answer
    
Thanks a lot Jessica! I got lost in minefield of arrays, much appreciated –  Craig Pearson Jun 20 '12 at 2:05
    
@CraigPearson Np, glad I could help. :) –  jessica Jun 20 '12 at 2:08
    
This helped a lot! Thanks!! –  Lucas Veiga Jul 18 at 0:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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