I am working on a website that has multiple check boxes and I want to show results based on what user has selected.
There are about 16 check boxes for music, 8 check boxes for venues and 6 radio buttons for area. The user can check a maximum of 3 check boxes from Music category, a maximum of 3 from Venues and 1 from Area. How do I accomplish this wordpress query that would show posts only from categories that are selected.
For example, If a user has selected House, Techno and Dubstep in Music, Miami as Area, and they want to search for events, music festivals, and live venues, How can I get that using wordpress query?
if(!empty($_POST)){
global $names;
$names = $_POST['music'];
$music = implode(",", $names);
$echo $music;
}
That will echo out all the selected checkboxes as house, dubstep, Miami, music festivals. Now I want to show posts that are in caegories of house, dubstep and are in music festivals category, and in miami area.
What I've used is
$args = array (
'category_name' => $music,
'posts_per_page' => 12,
'orderby' => 'title',
'order' => 'ASC',
);
$posts = query_posts($args);
But this will show all the posts from house, dubstep which is fine but it also shows all posts from music festivals that are not even in house and dubstep category.
Here is my HTML
code for checkboxes
<div class="customsearchul">
<div id="searchArea">
<ul>
<li><h4>Area:</h4></li>
<li>All of London<input type="radio" name="area" value="london" checked="london"/></li>
<li>North<input type="radio" name="area" value="north" /></li>
<li>East<input type="radio" name="area" value="east" /> </li>
<li>South<input type="radio" name="area" value="south" /></li>
<li>West<input type="radio" name="area" value="west" /></li>
<li>Central<input type="radio" name="area" value="central" /></li>
</ul>
</div>
</div>
<div class="customsearchul">
<div id="searchMusic">
<ul>
<li><h4>Music:</h4></li>
<li>House<input type="checkbox" name="music[]" value="house" /></li>
<li>Techno<input type="checkbox" name="music[]" value="techno" /></li>
<li>Trance<input type="checkbox" name="music[]" value="trance" /></li>
<li>Electronica<input type="checkbox" name="music[]" value="electronica" /></li>
<li>Drum and Bass<input type="checkbox" name="music[]" value="drum-and-bass" /></li>
<li>Garage<input type="checkbox" name="music[]" value="garage" /></li>
<li>Dubstep<input type="checkbox" name="music[]" value="dubstep" /></li>
<li>Trap<input type="checkbox" name="music[]" value="trap" /></li>
</ul>
</div>
</div>
<div class="customsearchul" style="margin-top: 34px;">
<div id="searchMusic">
<ul>
<li>Hip-Hop<input type="checkbox" id="music" name="music[]" value="hiphop" /></li>
<li>R'n'B<input type="checkbox" name="music[]" value="rnb" /></li>
<li>Rock<input type="checkbox" name="music[]" value="rock" /></li>
<li>Indie<input type="checkbox" name="music[]" value="indie" /></li>
<li>Reggae<input type="checkbox" name="music[]" value="reggae" /></li>
<li>Retro 80's/90's<input type="checkbox" name="music[]" value="retro" /></li>
<li>Party Bangers<input type="checkbox" name="music[]" value="party-bangers" /></li>
<li>Chart Hits<input type="checkbox" name="music[]" value="chart-hits" /></li>
</ul>
</div>
</div>
<div class="customsearchul">
<div id="searchType">
<ul>
<li><h4>Search Type:</h4></li>
<!--<li>Everything<input type="checkbox" name="occasion[]" value="debauchery" /></li> -->
<li>Nightclubs<input type="checkbox" name="occasion[]" id="nightclub" value="nightclub" checked="checked" /></li>
<li>Events & Tickets<input type="checkbox" name="occasion[]" value="club-guide" checked="checked" /> </li>
<li>Live Venues<input type="checkbox" name="occasion[]" value="festivals" checked="checked" /></li>
<li>Artist Interviews<input type="checkbox" name="occasion[]" value="artist" checked="checked" /></li>
</ul>
</div>
</div>
<!-- <input class="searchbutton" type="submit" value="Search" id="performsearch"/> -->
</form>
Thanks in advance for your help.
query_posts
) depending on the checkboxes. It's difficult to illustrate a working example without knowing your checkbox setup though – MBL Mar 20 '14 at 12:18