0

I'm having trouble querying posts that have multiple tags from a custom taxonomy. For example, if I have a post called "Strawberry Splice," it has the Meal Types (custom taxonomy) "Gluten Free" and "Beverages."

When I filter the posts to look for "Gluten Free," "Strawberry Splice" shows up but when I search for "Beverages," there aren't any results. My query is only looking at the first term.

Take a look at http://jimalie.com.au/recipes/

This is my partial code to generate the custom taxonomy:

$args = array( 
    'labels' => $labels,
    'public' => true,
    'show_in_nav_menus' => true,
    'show_ui' => true,
    'show_tagcloud' => true,
    'show_admin_column' => false,
    'hierarchical' => false,

    'rewrite' => true,
    'query_var' => true
);

register_taxonomy( 'meal_types', array('recipes'), $args );

This is my code for the query:

$mealtype = $_POST['mealtype'];

$args = array('post_type' => 'recipes', 'meal_types' => $mealtype, 'paged' => $paged);

I get $mealtype from the dropdown that is dynamically populated:

$terms = get_terms( 'meal_types' );
$count = count($terms);
if ($count > 0) {
    foreach ($terms as $term) { ?>
    <option value="<?php echo $term->name; ?>" <?php if($_POST['mealtype'] == $term->name) { echo 'selected'; } ?>><?php echo $term->name; ?></option>
    <?php }
}

My query:

$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();

The whole template's code: http://pastebin.com/5e07FMJm

How do I get it to look at all the terms?

2
  • 1
    What is the contents of $mealtype and how is it formatted? Also, how are you actully querying the posts - WP_Query(), get_posts() or query_posts()?
    – David Gard
    Mar 3, 2015 at 9:41
  • @DavidGard Updated my post with new info. I get $mealtype through the dropdown that is dynamically populated
    – Lindsey
    Mar 3, 2015 at 13:07

1 Answer 1

1

Got help on Facebook and just needed to change this:

<option value="<?php echo $term->name; ?>" <?php if($_POST['mealtype'] == $term->name) { echo 'selected'; } ?>><?php echo $term->name; ?></option>

to this:

<option value="<?php echo $term->slug; ?>" <?php if($_POST['mealtype'] == $term->slug) { echo 'selected'; } ?>><?php echo $term->name; ?></option>
1
  • Yes, the quick approach that you took (using {taxonomy_slug} => {term_slug} instead of tax_query uses the slug. Mar 3, 2015 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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