WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Complex query from a form submission (7 posts)

  1. eschmidt99
    Member
    Posted 1 year ago #

    Hello,

    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!
    Eric

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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',
    		array(
    			'taxonomy' => 'color',
    			'field' => 'slug',
    			'terms' => array( 'red', 'blue', 'yellow' )
    		),
    		array(
    			'taxonomy' => 'size',
    			'field' => 'slug',
    			'terms' => array( 'large', 'small' )
    		)
    	)
    );
    $query = new WP_Query( $args );
  3. eschmidt99
    Member
    Posted 1 year ago #

    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 . "',";
    endforeach;
    $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,
    Eric

  4. vtxyzzy
    Member
    Posted 1 year ago #

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

    'terms' => array( explode(',', $color) )
  5. eschmidt99
    Member
    Posted 1 year ago #

    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,
    Eric

  6. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  7. eschmidt99
    Member
    Posted 1 year ago #

    Hooray!

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic