I have added a custom taxonomy called experience to the page post-type, and the taxonomy has terms "vfds" and "plcs"

I am trying to create a search where a user can select one or more experience taxonomy terms from a select list, press the submit button, and see the results of a wp_query.

I need the query result to show pages with

1) the experience taxonomy has no (zero) terms selected

2) one or more terms selected

I have an html select list with an array experience[] to capture multiple selections:

<select name="experience[]" multiple="multiple">
<option value="vfds">VFDs</option>
<option value="plcs">PLCs</option>

Then I feed this array into a wp_query:


        $experience = $_GET['experience'];
        $args = array
                'post_type' => 'page',      
                'tax_query' => array(
                        'taxonomy' => 'experience1',
                        'field' => 'slug',
                        'terms' => $experience
    $query = new WP_Query( $args );

The query works fine in the sense that it will show pages that have one or more terms selected under the experience category.

The problem I am having is this:

IF a page has empty (no) values for the experience category, then it is not shown in the result list.

Is there a way where wp_query will show pages with

  1. no terms selected
  2. one or more terms selected

Thank you


Please let me add that in general I am trying to move away from using Drupal with Views. I am trying to replicate Views capability of being able to search using select fields with multiple values.

I have tried to use wp_query with a custom select field with multiple values, and failed miserably so far.

Hence I switched to using categories with multiple terms and am close to replicating a Views type search result.

If anyone knows any other way of doing what I would like to do in WP, I would very much appreciate it.

Thanks again!


I realize maybe my question does not make full sense. Let me try and explain further. The reason I need to search result to show pages with ZERO or more terms instead of ONE or more terms, is that I would like to combine the taxonomy search with a another field search, eg City. If a user fills in only a value for city, and no value(s) for experience terms, the search results will be zero because he did not fill in terms. Instead I need to show all pages with the city that was selected, whether or not any experience terms are selected

  • $args = array ( 'post_type' => 'page', 'tax_query' => array( array( 'taxonomy' => 'experience1', 'field' => 'slug', 'terms' => $experience ) ), 'meta_query' => array( array( 'key' => 'last_name ', 'value' => $lastname, 'compare' => 'LIKE' ) ) ); $query = new WP_Query( $args );
    – WPfan 1000
    Commented Apr 26, 2012 at 12:56
  • OP here - EG. This code above only shows results if BOTH lastname AND at least 1 term has been selected - I need it to show results if lastname has been selected and no terms have been selected: (Sorry cant seem to format this properly)
    – WPfan 1000
    Commented Apr 26, 2012 at 13:03
  • PLEASE EDIT YOUR Q INSTEAD OF ADDING COMMENTS ... I already moved one comment into the Q. Thanks.
    – kaiser
    Commented Apr 26, 2012 at 13:38

1 Answer 1


Seems like the easiest thing to do is to apply the tax query only if at least one "experience" term has been specified. Something like:

// Get passed vars
$experience = $_GET['experience'];

// Start building the args array
$args = array(   
    'post_type' => 'page',      

// If any experience items were passed
if( is_array( $experience ) && count( $experience ) > 0 ) {

    // Get an array of possible "experience" terms as a whitelist to check against.
    $arr_term_details = get_terms( 'experience1', array( 'hide_empty' => 0 ) );
    $arr_terms = array();
    foreach($arr_term_details as $this_term) {
        array_push($arr_terms, $this_term->slug);

    // If ALL of the experience terms exist in the whitelist
    if( count(array_intersect( $experience, $arr_terms )) == count($experience) ) {

        // Add the tax query
        $args['tax_query'] = array(
                'taxonomy' => 'experience1',
                'field' => 'slug',
                'terms' => $experience



// Run the query
$query = new WP_Query( $args );

The if condition may be wrong--I forget how forms pass values for multi-selects.

UPDATE: added a whitelist-style check for the $_GET data, per Chip Bennett's suggestion.

  • This is the approach I would take: conditionally building the arguments array. My only suggestion would be to be sure to sanitize the $_GET data. Commented May 30, 2012 at 22:04
  • @ChipBennett What sanitizing function would you recommend?
    – MathSmath
    Commented Jun 1, 2012 at 15:13
  • If you know the valid values, I would do an in_array() or array_key_exists(), depending on how you define those valid values. Commented Jun 1, 2012 at 15:17
  • @ChipBennett Updated based on your suggestion. Was a bit harder than I figured, since there's no WP function to get JUST term slugs, and since the $_GET data is also an array (I'm assuming anyway, since it's a multi-select list of terms). I ended up doing an array_intersect(), but there might be a simpler way? In any case, good call.
    – MathSmath
    Commented Jun 1, 2012 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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