Home Forums ACF PRO Meta query doesn't work after update_field until manual update in admin


Meta query doesn't work after update_field until manual update in admin

  • So, I have a function running if certain conditions are true, to add a post (custom post type), and update some ACF fields of the newly created post with this code:

    $balance_post = array();
    $balance_post['ID'] = 0;
    $balance_post['post_status'] = 'publish';
    $balance_post['post_type'] = 'ik_balance';
    $balance_post['post_title'] = $account->post_title . ' - ' . $transaction_date . ' - ' . $updated_balance;
    $balance_post_id = wp_insert_post($balance_post, true);
    update_field( 'field_5f1ddb03428dc', $updated_balance, $balance_post_id );
    update_field( 'field_5f1ddb03428c8', $transaction_date, $balance_post_id );
    update_field( 'field_5f1ddb03428bf', $account, $balance_post_id );

    This works fine, the post is created and published. Editing the post in wp-admin shows the correct content in the fields. I can find the data in the database, and even get_field() works fine.

    But when I run a wp_query with a meta_query to retrieve this newly created post it just isn’t found until I go to wp-admin, edits the post (without changing any field) and just clicking Update.

    Then it suddenly works as expected and the post is retrieved with the query.

    This is the query I’m using:

    $args = array(
    	'post_type'     => 'ik_balance',
    	'post_status'   => 'publish',
    	'posts_per_page' => 1,
    	'fields'        => 'ids',
    	'meta_key'  => 'balance_date',
    	'orderby'   => 'meta_value_num',
    	'order'     => 'DESC',
    	'meta_query'	=> array(
    		'relation' => 'AND',
    			'key'	 	=> 'balance_account',
    			'value'	  	=> $account_id,
    			'compare' 	=> '=',
    $balance_query = new WP_Query( $args );

    What am I missing?

  • I do not see how these are set $updated_balance, $transaction_date or $account or what type of data these values have.

    Also how any of these relate to the balance_account field or what type of field this is and what $account_id represents.

  • Thanks for your response @hube2 ! I’ll try to fill in:

    The fields are set like this:

    $updated_balance = get_field('updated_balance', $post_id);
    $transaction_date = get_field('transaction_date', $post_id);
    $account = get_field('transaction_account', $post_id);

    Where updated_balance is a number field, where transaction_date is a date field and transaction_account is a post object field selecting a ik_account CPT.

    All these fields are set on a new ik_balance CPT post when a ik_transaction CPT post is saved.

    So, in the query, I’m first looping through all posts of the ik_account CPT, getting only the post ID which is what’s populating $account_id. Inside that loop, the above query fetches the last ik_balance CPT where the balance_account post object field equals $account_id.

    Hope this makes things at least somewhat more clear. Otherwise I’ll try to clarify further!

  • I don’t know if this is the cause of your problem or not

    $account = get_field('transaction_account', $post_id);

    returns a post object and not just the ID unless you have the field setting to return just the ID.

    This may or may not return an array of post IDs it it allows multiple values.

    To get just the post ID at this point you can also do

    $account = get_field('transaction_account', $post_id, false);

    This following expects the value to be a post ID or an array of post IDs depending on if the post object can hold multiple selections or not.

    update_field( 'field_5f1ddb03428bf', $account, $balance_post_id );

    A am assuming that this is saving the full post object into the field when load the field in the admin it is seeing that it is a full post object and correcting the saved value when you save.

  • Hmm, that could’ve been it, but changing $account to $account->ID doesn’t make a difference for the issue.

  • If it’s being updated with the right value then I haven’t got a clue why your query is not working. My next step would be to look in the database to see exactly what is being saved in that field.

  • Heureka! Your suggestion got me on the right track, it was the date field formatting that was the issue. My date field that was setting the $transaction_date was setup to return Y-m-d, but when setting a date field with update_field it must be in Ymd format. So a simple conversion before update_field did the trick!

    Thanks @hube2 for leading me on the right track! 🙂

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

You must be logged in to reply to this topic.