0

I have a CPT with post type of 'recipe'. I have several ACF Advanced Custom Fields in the CPT.

My loop WORKS correctly when it is written like this:

<?php

// args
$args = array(
    'numberposts'   => -1,
    'order'         => 'ASC',
    'orderby'       => 'name',
    'post_type'     => 'recipe'
);

// query
$recipe_query = new WP_Query( $args );

?>
<?php if( $recipe_query->have_posts() ): ?>

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

        <?php get_template_part( 'template-parts/content', 'recipe' ); ?>

    <?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post().

?>

The 'recipe' CPT has an ACF field (field type: select) named 'special_diets', with several values like 'vegan', 'vegetarian', 'sugar-free', etc. Multiple selections are allowed in the field. (Most recipes have multiple values selected.)

When I add arguments to the $args array to try to show only posts that have one of the special diet types, the loop returns nothing:

// args
$args = array(
    'numberposts'   => -1,
    'order'         => 'ASC',
    'orderby'       => 'name',
    'post_type'     => 'recipe',
    'meta_key'      => 'special_diets',
    'meta_value'    => 'vegan'
);

I have tried adding a meta_compare as well, using 'IN' and also trying '='

// args
$args = array(
    'numberposts'   => -1,
    'order'         => 'ASC',
    'orderby'       => 'name',
    'post_type'     => 'recipe',
    'meta_key'      => 'special_diets',
    'meta_value'    => 'vegan',
    'meta_compare'  => 'IN'
);

But as soon as I add the 'meta_key' at all, the loop returns nothing (and no error messages). I've double- and triple-checked that the field name 'special_diets' is correct.

What's wrong?

This code:

<pre>
    <?php
        var_dump(get_field('special_diets'));
    ?>
</pre>

returns:

array(7) {
  [0]=>
  string(10) "sugar-free"
  [1]=>
  string(11) "gluten-free"
  [2]=>
  string(8) "egg-free"
  [3]=>
  string(10) "dairy-free"
  [4]=>
  string(10) "vegetarian"
  [5]=>
  string(5) "vegan"
  [6]=>
  string(17) "diabetic-friendly"
}
0

you need to add a LIKE in your meta_query

$args = array(
    'post_type'=> 'recipe',
    'meta_query' => array(
                     array(
                        'key'     => 'special_diets',
                        'value'   => 'vegan',
                        'compare' => 'LIKE',
                    )
                ),
);
$query = new WP_Query($args);

as explained here Count custom post types with a specific meta value

  • This does seem to work, thanks Gregory. Are the commas correct? The close parenthesis of the first meta_query array is the final argument in the $args array, so technically that should not have a comma, right? – hommealone Jun 19 at 22:53
  • there where an extra line from linked page ^^ – Gregory Jun 19 at 23:52
  • now you just need to add your while or foreach with this query – Gregory Jun 19 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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