Post Query with Meta Key stopped working

[ad_1]

Hello,

I am working on a magazine website that has a large amount of posts, submissions, etc. There is about 60K+ posts/submissions on the site right now. I don’t know when it happened specifically, but today I noticed that my featured posts section has stopped working.

The functionality is simple. When editing a post there is a checkbox on the side what when you check it, an ACF meta key by the name “featured\_post” becomes true. Then in the featured post section, my WP Query runs:

$args = [
‘numberposts’ => 1,
“posts_per_page” => 1,
‘post_type’ => ‘post’,
‘meta_key’ => ‘featured_post’,
‘meta_value’ => true
];

$the_query = new WP_Query($args);

Like I said, this WAS working and now it isn’t returning anything. I commented out the meta\_key and meta\_value arguments and it returns posts. I have also tried adding ‘meta\_compare’ => ‘LIKE’ / ‘=’ as a last argument and that did not work.

I have tried rewriting the meta as a meta\_query:

‘meta_query’ => array(
array(
‘key’ => ‘featured_post’,
‘value’ => true
)
)

I’ve also tried including a compare argument after the ‘value’ argument too:

‘compare’ => ‘LIKE’
and also tried
‘compare’ => ‘=’

I’ve also tried using square brackets instead of calling array(). And tried calling the meta\_value as 1 and ‘1’ instead of true.

And I have also tried wp\_reset\_postdata(), wp\_reset\_query(), and even remove\_all\_actions(‘pre\_get\_posts’) at the same time, different times, and a mix.

I have also looked in the database and I get see the meta\_value in the postmeta table.

I am absolutely at a loss as to why this stopped working. Any help is appreciated.

[ad_2]
1 Comment
  1. What is your ACF field type set as? Have you tried turning on the debug.log and looking for any errors there?

    I do this myself extremely often with a ACF true/false field to mark posts/things as “featured” or what not to pull in on other pages. My query is just like yours with the only difference being my meta_value is set to 1.

    `’meta_key’ => ‘featured’,`

    `’meta_value’ => ‘1’,`

    Edit: What is “numberposts” ? I don’t think I’ve ever seen that before and looking up WP_Query on the [codex]) shows no reference to such an option that I can find. Is that maybe your problem? You also have double quotes around your posts_per_page, those should be single.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer