I’ve been reading a lot how you can’t use wp_insert_post and update_field because the field isn’t created yet or sum such, so I’ve experimented with MANY permutations of wp_insert_post.

My field is a User object and I’m inserting the User ID. I have multiple values turned on so I update with an array of User IDs with an array of just one entry. There will be multiple entries in the array in the future.

Here’s what I’ve tried:

wp_insert_post and then update_field with just the field name

wp_insert_post and then update_field with the field key

wp_insert_post and using update_post_meta with just the field name

wp_insert_post and using update_post_meta to insert the field name and field key with the underscore version of the field name

wp_insert_post and using add_post_meta with the field name and the field key with the underscore version of the field name

wp_insert_post with the meta_input in the arguments with both the field name and the field key with the underscore version of the field name

Here’s what happens:
The field gets inserted. You can see it in the WP post editor that it was successfully created the right way. Going to the database using PHPMyAdmin reveals that is was added.

Now the weirdness:
I’m running THIS query to find out what I need. The query runs on a polling process every 15 seconds to find a new message for user X.

$args = array(
  ‘numberposts’ => -1,
  ‘post_status’ => ‘publish’,
  ‘post_type’   => ‘message’,
  ‘meta_query’ => array(
       ‘relation’ => ‘AND’,
              ‘key’ => ‘message_recipients’,
              ‘value’ => get_current_user_id(),
              ‘compare’ => ‘LIKE’
              ‘key’ => ‘message_read_by’,
              ‘value’ => get_current_user_id(),
              ‘compare’ => ‘NOT LIKE’

What happens is that the query doesn’t return ANY rows. BUT, when you go into the WP post editor and manually UPDATE the post, THEN and ONLY THEN does the record get found.

I’ve actually tried pulling out the SQL that gets generated from this request and it behaves THE SAME WAY. No rows found until you click UPDATE.

After the record is found, the cleanup happens when my code again runs update_post_meta to insert the user id in the “message_read_by” field, which effectively removes it from the query results because it then doesn’t fit into the second search parameter. So that time IT WORKS. If you don’t touch the data with the WP post editor then it works? This doesn’t make sense.

I need this to work with programmatic inserts without the WP post editor. The data looks the same! Here’s the database view:

The initial programmatic update with the insert. https://i.stack.imgur.com/us1rU.jpg

After I click “update” on the post: https://i.stack.imgur.com/3rkn1.jpg

It’s the same data! So why does the query only work after I click update?

The wp_posts table is also the same, except the author user id changes from 3 to 0. I’ve also tried updating that field in the database, but that wasn’t it either.

Is there another field or something that gets changed or updated when I click the UPDATE button in the WP post editor? That’s all I can think of.

  • Well, there are other database differences in the wp_posts table. For instance the modified date, creation of a post revision, etc. In your screenshots, notice that the left column values are different by 1, so not exactly the same like you thought. If that is the post ID, you could be having an issue where it's pointing at the wrong version of the post.
    – tmdesigned
    Mar 29, 2019 at 11:09

1 Answer 1


Hello and thanks for all who read this and commented.

Apparently, the query didn't return any rows because BOTH FIELDS need to be present for the query to run correctly. The secondary meta parameter "message_read_by" didn't exist so I just needed to insert with no value.

Problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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