最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - ACF meta_key and meta_value break loop

programmeradmin1浏览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"
}

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"
}
Share Improve this question asked Jun 19, 2019 at 22:09 hommealonehommealone 851 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论