Support » Fixing WordPress » Complex query from a form submission

  • Resolved eschmidt99



    I hope this makes sense and I hope someone can help.

    Here’s the basic situation:

    I have a custom post type with multiple category-style taxonomies.

    On the form page where users begin their search, I have a form with multiple fieldsets, each fieldset containing multiple checkboxes (more than one checkbox can be selected for each fieldset). Each fieldset is associated with one of the taxonomies.

    A sample form where the user wants to search for a specific Widget:

    Fieldset 1 contains the categories from the taxonomy ‘Color’.
    Fieldset 2 contains the categories from the taxonomy ‘Size’.

    Now, let’s say the user checks the following boxes in the different fieldsets:

    Color: ‘Red’, ‘Blue’, ‘Yellow’.
    Size: ‘Small’, ‘Large’.

    What I would like to accomplish is query the Widget posts and return all the Widgets that are (Red OR Blue OR Yellow) AND (Small OR Large).

    This could, in theory, return up to 6 Widgets, those that are:
    Red and Small, Red and Large, Blue and Small, Blue and Large, Yellow and Small, Yellow and Large.

    Thanks for any help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You will need to construct a tax_query. Here is an example shown in the Codex, slightly modified for your example (untested):

    $args = array(
    	'post_type' => 'widget',
    	'tax_query' => array(
    		'relation' => 'AND',
    			'taxonomy' => 'color',
    			'field' => 'slug',
    			'terms' => array( 'red', 'blue', 'yellow' )
    			'taxonomy' => 'size',
    			'field' => 'slug',
    			'terms' => array( 'large', 'small' )
    $query = new WP_Query( $args );

    Thank you vtxyzzy for the code. It seems to be doing what I want when I enter the terms in manually. However, since I’m getting the array term values from a form, I need to enter the terms into the tax_query from a variable. As it is, to build the variable, I have:

    foreach ($_POST['color'] as $item) :
    	$item = filter($item);
    	$color .= "'" . $item . "',";
    $color = substr_replace($color ,"",-1);

    When I echo out $color, I get something like “‘red’,’blue’,’yellow'”, without the double quotes, as expected. But when I enter in

    'terms' => array($color)

    Nothing is returned. But if I enter the terms in manually, I get the posts as expected.

    So, my question is: How do I pull the terms array from a variable?

    Thank you again for your help,

    $color is a single variable. The array requires each element to be a variable. Try this:

    'terms' => array( explode(',', $color) )

    Thanks again for your time, I really appreciate it. When I enter your code I get the following error:

    Warning: strip_tags() expects parameter 1 to be string, array given in ..\wp-includes\formatting.php on line 955

    And wouldn’t explode() remove the commas?

    Thanks again,

    Removing the commas is the whole point. However, I did not get it quite right. You do not need the single quotes around each color, so change this line:

    $color .= "'" . $item . "',";

    to this:

    $color .= $item . ",";

    and this line:

    'terms' => array( explode(',', $color) )

    to this:

    'terms' => explode(',', $color)

    You could also just put the colors into an array in the foreach loop.

    One other point, if it is possible that no colors are checked, you should use a default.


    That worked. Hopefully this page ranks high in the SE’s as I couldn’t find an answer to this problem anywhere even though it had been asked a few times.

    Thanks a ton vtxyzzy! You’re my hero.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Complex query from a form submission’ is closed to new replies.