Support

Account

Home Forums ACF PRO wp_insert_post and WP Meta Query

Helping

wp_insert_post and WP Meta Query

  • 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’,
    array(
    ‘key’ => ‘message_recipients’,
    ‘value’ => get_current_user_id(),
    ‘compare’ => ‘LIKE’
    ),
    array(
    ‘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://imgur.com/BN9DvGi

    After I click “update” on the post:
    https://imgur.com/GnwIgoF

    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.

  • Hello and thanks for all who read this.

    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. Please mark it as such.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘wp_insert_post and WP Meta Query’ is closed to new replies.