I'm stumped by what seems like a simple task: I want to randomly select a post that fits certain criteria (metadata set by a specific plugin) and then return that random post to the plugin in question. This plugin has a filter that appears perfect for the task, and if I hardcode the post ID all works well.
What's odd is I can't seem to pull the posts I need using WP_Query
. I know the metadata is there, I can see it in phpmyadmin. I can query other metadata, just not the metadata set by this plugin.
How can I troubleshoot / further investigate this odd situation? I can't think of any reason why only SOME metadata could be accessed by WP_Query
.
Just for reference, here's the code from my filter / query.
function boxzilla_randomize( $load, $box_id ) {
$box_args = [
'post_status' => 'publish',
'meta_query' => [
[
'key' => 'boxzilla_options',
'compare' => 'EXISTS'
]
]
];
$box_query = new \WP_Query( $box_args );
if ( $box_query->has_posts() ) {
write_log("hey, I got one");
}
// return $box_id;
}
add_filter( 'boxzilla_load_box', __NAMESPACE__.'\\boxzilla_randomize', 10, 2 );
Edit for the purpose of clarifying the question: I see that the metadata exists in the db but a post query always comes up empty.
WP_Query
is slightly blunt tool for the job but for now I would be happy just successfully retrieving the right posts from the db.echo $box_query->request;
- is it good? You can also copy the SQL and run it via phpMyAdmin and see if you're getting the expected results that you wanted.boxzilla_options
come from? Given that Boxzilla is a 3rd party premium plugin, have you contacted their support? This Q might be saveable if it's just about grabbing a random post from a query, but if any knowledge at all about boxzilla is required then the Q has to be closed as off topic, and right now there are no boxzilla docs that document this. Can you confirm that the key is exactlyboxzilla_options
?