I'm having a bit of trouble getting a custom post type query working coming from two drop downs. Let me show you what I'm using:
The Form:
<?php require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); ?>
<form method="post" id="searchform" action="http://www.findabuilder247.com/directory">
<?php $args = array(
'show_option_none' => 'Select a Tradesman',
'name' => 'catagory',
'post_type' => 'profiles',
'hide_empty' => '1'
); ?>
<?php wp_dropdown_categories( $args ); ?>
<?php $args = array(
'show_option_none' => 'Select Area',
'name' => 'area',
'show_count' => '1',
'taxonomy' => 'areas',
'orderby' => 'name',
'post_type' => 'profiles',
'hide_empty' => '1'
); ?>
<?php wp_dropdown_categories( $args ); ?>
<input type="submit" value="" class="searchbutton4" />
</form>
I'm then generating the query (or at least attempting to) with:
<?php
if($_POST){
$query = $_POST;
$cats = array();
foreach($query as $key => $param) {
if($param != -1) {
array_push($cats, $param);
}
}
// print_r($cats); // This prints the array, so I know it's working
query_posts(array( 'category__and' => $cats ));
} ?>
I'm using a custom loop to call the query in initially and I'm hoping to use this as a search function.
<?php $loop = new WP_Query( array( 'post_type' => 'profiles', 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
...
<?php endwhile; ?>
Any help would be much much appreciated. I'm pulling my hair out at this!
UPDATE
James Kemp is an absolute master! The following code is working perfectly for me:
<?php $loop = new WP_Query( array( 'post_type' => 'profiles', 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' ) ); ?>
<?php
if($_POST){
$query = $_POST;
$cats = array();
foreach($query as $key => $param) {
if($param != -1) {
array_push($cats, $param);
}
}
$args=array(
'category__in' => $cats,
'post_type' => 'profiles',
'posts_per_page' => 10,
'order'=> 'ASC',
'orderby' => 'title'
);
$loop= null;
$loop = new WP_Query($args);
} ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
…
<?php endwhile; ?>