Wordpress: get_post() results don't take into account ACF field updates done with update_field()

Viewed 37

I've got an automated process that updates an ACF field for a product with the update_field() function.

update_field('event_start_datetime_utc', $new_event_datetimestamp, $product_id);
$product = wc_get_product( $product_id );
$product_name = $product->get_name();
$product->set_purchase_note($product_name . ': ' . $new_event_date);
$product->set_stock_status('instock');
$product->save();

When I perform a get_post() query, the new value stored in 'event_start_datetime_utc' is not taken into account.

$meeting_ids = get_posts([
    'posts_per_page' => -1,
    'post_status' => array('publish', 'private'),
    'post_type' => 'product',
    'meta_query' => array( 'main_query' => array(
            'key' => 'event_start_datetime_utc',
            'compare' => 'EXISTS'
        )
    ),
    'meta_key' => 'event_start_datetime_utc',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'fields' => 'ids',
]);

I need to log in to WordPress Dashboard, manually select the product, and click the "Update" button. Only then the updated data in event_start_datetime_utc ACF field is taken into account by the get_post() query.

I already did some research online and tried some fixes suggested in other questions. Unfortunately, none of the following additional commands to update the product with PHP are solving my issue.

$product->update_meta_data( '_sync_updated', true );
wc_delete_product_transients( $product_id );
WC_Cache_Helper::get_transient_version( 'product', true );

Any idea on how to solve the problem?

0 Answers
Related