Trying to Filter Posts in WordPress Through a Form | category__and()

michaelsmith1994 used Ask the Experts™
I acknowledge from the start that this is a hack and then some.  I'm fully open to anything that can improve what I'm doing.

I have a form on a page in WordPress.  

It has two pulldown <select> menus and a set of checkbox options.

Each of the inputs has a value="" of some number that corresponds to a WordPress category.

The intended function of my form once it's submitted is to show only pages that have all of the selected categories.  That's the important part.  The pages must have all of the categories selected.  If the user chooses categories 3,4, and 18, then I want to only show the posts that are associated with categories 3,4, and 18.  If there aren't any that match, that's obviously fine.

Here's the form:

<form method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>" id="trip-finder-form">
<select name="region" id="region">
<option value=" ">All Regions</option>
<option value="41">Andalusia</option>
<option value="42">Asturias</option>
<option value="43">Catalonia</option>
<option value="44">Galicia</option>
<option value="45">Valencia</option>	

<select name="month" id="month">
    <option value=" ">All Months</option>
    <option value="17">January</option>
    <option value="18">February</option>
    <option value="19">March</option>
    <option value="20">April</option>
    <option value="21">May</option>
    <option value="22">June</option>
    <option value="23">July</option>
    <option value="24">August</option>
    <option value="25">September</option>
    <option value="26">October</option>
    <option value="27">November</option>
    <option value="28">December</option>

<input name="check_list[]" type="checkbox" value="30" /><span>Active Adventures</span></label>
<input name="check_list[]" type="checkbox" value="31" /><span>Birding Trips</span></label>
<input name="check_list[]" type="checkbox" value="32" /><span>Cross Border</span></label>
<input name="check_list[]" type="checkbox" value="33" /><span>Cultural Journeys</span></label>
<input name="check_list[]" type="checkbox" value="34" /><span>Family Adventures</span></label>
<label><input name="check_list[]" type="checkbox" value="35" /><span>Festivals</span></label>
<label><input name="check_list[]" type="checkbox" value="36" /><span>Group Expeditions</span></label>
<label><input name="check_list[]" type="checkbox" value="37" /><span>Private Journeys</span></label>
<label><input name="check_list[]" type="checkbox" value="38" /><span>Signature Journeys</span></label>
<label><input name="check_list[]" type="checkbox" value="39" /><span>Tailored Journeys</span></label>

<input type="hidden" name="updated" value="true" />

<input type="submit" value="Explore Our Trips">

Open in new window

This form resides on a PHP page and, once it has been submitted, there's some code that takes action.  If form fields were set, those values are grabbed and added to an array.  Then, those array values are put together into a comma-separated string and fed to an array of parameters for wp_query, specifically into category__and().  

According to what I've read, the parameter 'category_and' should accept an array of category values and do just what I'm looking for.  

I can't get this to work for the life of me.

If I choose just one option, from any of the two select menus or the checkboxes, this works fine.  If I choose more than one option, it falls apart.  It returns results that are clearly not right.  

I'll choose Valencia as my first pulldown.  Works fine.

If I choose Valencia and July, let's say, I'll get trips to other regions even though they definitely don't have the Valencia category associated with them.

$my_query = new WP_Query(array(
	'post_type' => array('page'),
	'category__and' => array($parameters)

Open in new window

I've put a lot of time and a lot of reading into this.  Not sure what to do.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Have you tried:
$query = new WP_Query( array(
	'post_type' => array( 'post', 'page' ),
	'category__and' => array($parameters),
	//'posts_per_page=-1', //Display all posts in one page
	'nopaging=true' //Display all posts by disabling pagination
) );

Open in new window

Thanks for the response.

I actually found help here just this morning:

I think the issue had to do with the datatype I was handing off to 'category__and()'.  Modifying that before passing the parameters off to category__and() got me what I needed.
Top Expert 2012

Glad to see you got it working.
In your first Post you have also omitted the closing bracket of ... new WP_Query(


I found a solution in the WordPress forums.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial