drake035
drake035

Reputation: 2897

Advanced custom fields: can't query posts by custom field

I'm trying to query posts whose ACF field "show_on_frontpage" value is equal to "yes" (see definition of this field in screenshot below). As prescribed in ACF docs here's my code:

$args = array(
  'posts_per_page' => -1,
  'meta_key' => 'show_on_frontpage',
  'meta_value' => 'yes'
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
  while ($my_posts->have_posts()) : $my_posts->the_post();
    if (get_field('show_on_frontpage')) the_field('show_on_frontpage'); ?>
  endwhile;
}

This returns/displays nothing. If I used instead simply $args = array('posts_per_page' => -1); then I get all my posts and "yes" shows up for those that have "yes" as the value of their "show_on_frontpage" field.

What's wrong with my code?

enter image description here

Upvotes: 0

Views: 1925

Answers (2)

Joe
Joe

Reputation: 1822

According to this question/answer on the ACF forum:

https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/

It would be better to switch your checkbox field to a True/False field instead, since it appears that your checkbox group field only contains a single option.

Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field.

If you use a true/false field then you can use WP_Query, the values of a true/false field are 0 (zero) for false and 1 for true.

So if you switched your checkbox field to a True/False field, you would rewrite your code as follows:

$args = array(
    'posts_per_page' => -1,
    'meta_key' => 'show_on_frontpage',
    'meta_value' => 1 /* or true */
);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) {
    while ($my_posts->have_posts()) : $my_posts->the_post();
        /* My content for each post with the checkbox checked goes here */
    endwhile;
}

Upvotes: 2

csknk
csknk

Reputation: 2039

This should work if you use the more recent meta_query => array() syntax:

$args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'show_on_frontpage',
                'value' => 'yes',
                'compare' => 'LIKE',
            )
        ),
    );

    $my_posts = new WP_Query($args);

    if ($my_posts->have_posts()) {

        while ($my_posts->have_posts()) : $my_posts->the_post();

          echo get_the_title();
          // Post stuff

        endwhile;

        /* Restore original Post Data */
        wp_reset_postdata();

}

Note that you need to give the post ID to the ACF helper functions get_field() & the_field() within the while loop.

See https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

On a wider note, this article calls into question the wisdom of using post_meta keys for this purpose and is worth a read: https://tomjn.com/2016/12/05/post-meta-abuse/. The article suggests using a custom taxonomy to achieve what you need - being better for performance.

Upvotes: 0

Related Questions