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.
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.
Share Improve this question edited Oct 17, 2019 at 20:05 jamesfacts asked Oct 17, 2019 at 16:30 jamesfactsjamesfacts 17711 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 0Your query should work as is. To test this, I created 4 posts, and then using WP CLI, ran:
wp post meta add 10 boxzilla_options "testingtesting123"
Where post 10 is a post with the title test. 3 other posts existed that did not have the post meta.
To test that the post meta key/value pair did indeed exist, I ran this code:
wp> get_post_meta( 10 );
=> array(2) {
["_edit_lock"]=>
array(1) {
[0]=>
string(12) "1571337707:1"
}
["boxzilla_options"]=>
array(1) {
[0]=>
string(17) "testingtesting123"
}
}
I then opened wp shell
and ran this code:
wp> $args = ['post_status' => 'publish', 'meta_query' => [['key' => 'boxzilla_options','compare' => 'EXISTS']] ];
=> array(2) {
["post_status"]=>
string(7) "publish"
["meta_query"]=>
array(1) {
[0]=>
array(2) {
["key"]=>
string(16) "boxzilla_options"
["compare"]=>
string(6) "EXISTS"
}
}
}
wp> $posts = get_posts( $args );
The resulting $posts
array contained the desired post with ID 10:
wp> $posts = get_posts( $args );
=> array(1) {
[0]=>
object(WP_Post)#1903 (24) {
["ID"]=>
int(10)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2019-10-17 18:44:10"
["post_date_gmt"]=>
etc...
Thus, the problem is likely the Boxzilla plugin, which means that further help on WPSE is not possible. You should contact Boxzilla support
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. – jamesfacts Commented Oct 17, 2019 at 16:53echo $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. – Sally CJ Commented Oct 17, 2019 at 17:12boxzilla_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
? – Tom J Nowell ♦ Commented Oct 17, 2019 at 17:19