Support

Account

Home Forums ACF PRO Meta query doesn't work after update_field without manually saving in backend

Unread

Meta query doesn't work after update_field without manually saving in backend

  • 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',
    
    									array(
    										'key'	 	=> 'balance_account',
    										'value'	  	=> $account_id,
    										'compare' 	=> '=',
    
    									)
    								),
    							  );
    
    							  // The Query
    							  $balance_query = new WP_Query( $args );

    What am I missing?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.