Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; ?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You seem to be using 2 separate queries there, the first (query_posts) won't do anything, and the second is the query that will be used. Have you tried:

$args=array(
  'category__in' => $cats,
  'post_type' => 'profiles',
  'posts_per_page' => 10,
  'order'=> 'ASC',
  'orderby' => 'title'
  );
$loop= null;
$loop = new WP_Query($args);

Try that instead of:

query_posts(array( 'category__and' => $cats ));

and

<?php $loop = new WP_Query( array( 'post_type' => 'profiles', 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' ) ); ?>
share|improve this answer
    
You absolute wizard! Modify that a little to include the call inside the post and it's working perfectly. Owe you one :) –  Stephen Radford Dec 9 '11 at 9:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.