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.

Empty results querying db

Meta key visible in phpmyadmin

  • So you want to fetch posts via their post meta values, and randomly select one of the posts retrieved? Keep in mind that ordering things randomly is extremely slow/expensive, and querying things via their post meta values is also very expensive, though not as much as random ordering. Usually when people want a random post, they don't really mean truly random and mean something else, e.g. show a different post each time I refresh the page, but distribute the selection evenly
    – Tom J Nowell
    Oct 17, 2019 at 16:48
  • Yes, that's exactly right, I'd like to select one of the (2-3 possible) posts with a certain meta value. It's true, the 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
    Oct 17, 2019 at 16:53
  • Regarding the troubleshooting, try inspecting the SQL query - 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.
    – Sally CJ
    Oct 17, 2019 at 17:12
  • The query isn't the issue, the query does nothing random because it's not told to, if it produced the desired results with the given options that would be a bug. Instead, where does 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 exactly boxzilla_options?
    – Tom J Nowell
    Oct 17, 2019 at 17:19
  • 1
    @SallyCJ you are 100% correct—the post type turns out to be a custom post type, which I did not anticipate. That was the issue I had all along!
    – jamesfacts
    Oct 18, 2019 at 21:55

1 Answer 1


Your 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) {
  array(1) {
    string(12) "1571337707:1"
  array(1) {
    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) {
  string(7) "publish"
  array(1) {
    array(2) {
      string(16) "boxzilla_options"
      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) {
  object(WP_Post)#1903 (24) {
    string(1) "1"
    string(19) "2019-10-17 18:44:10"

Thus, the problem is likely the Boxzilla plugin, which means that further help on WPSE is not possible. You should contact Boxzilla support

  • Thank you for double-checking my syntax, and I'm grateful for the wp shell demo, I didn't realize the CLI offered that feature. You're absolutely right it must be something in Boxzilla, I just wasn't sure if there was a subtle requirement in building meta queries I might have missed.
    – jamesfacts
    Oct 17, 2019 at 19:56
  • Yeah you were on the right track, and it should have worked for you, whatever's going on is some sort of interference
    – Tom J Nowell
    Oct 17, 2019 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.