Link to home
Start Free TrialLog in
Avatar of axessJosh
axessJosh

asked on

WP_Query multiple custom taxonomies

I am using the following code to run a query based on drop down selections from custom taxonomies in a custom post.

The query is sending correctly to the page, however, the results are not displaying and I cannot figure out how to change the line of code that is creating the conflict.  Here is the code.

<?php 

$list = array();

$item = array();

foreach($_POST as $key => $value){

if($value != ''){

$item['taxonomy'] = htmlspecialchars($key);

$item['terms'] = htmlspecialchars($value);

$item['field'] = 'slug';

$list[] = $item;

}

}

$cleanArray = array_merge(array('relation' => 'AND'), $list);


$args['post_type'] = 'small-groups';

$args['showposts'] = 9;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args['paged'] = $paged;

$args['tax_query'] = $cleanArray;

$the_query = new WP_Query( $args );

 print_r($cleanArray);
?>
<?php echo ($the_query->found_posts > 0) ? '<h3 class="foundPosts">' . $the_query->found_posts. ' listings found</h3>' : '<h3 class="foundPosts">We found no results</h3>';?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post();?>

<?php the_title(); ?><br />

<?php endwhile; wp_reset_postdata();?>

<div class="row page-navigation">

<?php next_posts_link('« Older Entries', $the_query->max_num_pages) ?>

<?php previous_posts_link('Newer Entries »') ?>

</div>

Open in new window


The line causing the conflict is:

$cleanArray = array_merge(array('relation' => 'AND'), $list);

Since i do not have posts that satisfy all the items in the selection.  I have changed "AND" to "OR" but that still doesn't work as desired.

Is there a way I can update that line of code to get posts that match any of the criteria listed?
SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of James Rodgers
James Rodgers
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of axessJosh
axessJosh

ASKER

Thanks for both answers.  Looks like we're heading in the right direction.

@JamesRodgers, I tried your code but got the same result.  Let me clarify my question a bit in case that makes a difference.

I have setup a series of drop down Select fields.  They pull the data from our Custom Taxonomies (categories) in my custom posts i setup.  

For simplicity sake, I have Meeting Time and Age Range.

With the AND statement, it seems that all options have to be selected for it to find any posts that meet that criteria.  If one select is left blank, then no results are found.  I have confirmed that posts exist for the criteria.

When I change the relation to "OR", I am able to get results from the posts, but by nature of the OR relationship, it doesn't allow the results to filter by the other criteria.

I am almost thinking that perhaps there needs to be a null value if nothing is selected from a Select Field that perhaps could be causing the issue.

Thoughts?
Perhaps the form information would help as well.  I'd be open to scrapping this array information and setting up another new wp_query.

<div class="lg-form"> 
<form method="post" action="?page_id=187" >
<?php $taxonomies = get_object_taxonomies('small-groups');

foreach($taxonomies as $tax){

echo buildSelect($tax);

}

?>
<li class="last-child">
<input type="submit" value="Search Groups" name="search-groups" id="search-groups"/>
</li>
</ul>
</form>
</div>

Open in new window

Ugh!

After all that, it appears the issue was with my input type="submit".

I had given it an Id and Name, both of which then showed up in the array.  Once I removed the Id and Name, the query works as expected.

Thanks for your time and great responses!