meta query – acf/save_post affecting WP_Query results

Question

I have CPT Events, which lists from WP_Query and orders by meta query where events that have date/time less then current are not shown + ordered by most closest events to further.

Array
(
    [post_type] => tplus_event
    [posts_per_page] => 8
    [category_name] => 
    [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => import_fields_order_by_start_date
                    [value] => 2022-12-15 15:12:58
                    [compare] => >=
                    [type] => DATETIME
                )

        )

    [order] => ASC
    [orderby] => meta_value
)

These arguments come from array nest because I need to separate args for events & posts:

$args_array['meta_query'] = [
    array(
        'key' => 'import_fields_order_by_'. THEME_OPTIONS['event_settings']['order']['order_by'] .'_date',
        'value' => current_datetime()->format('Y-m-d H:i:s'),
        'compare' => '>=',
        'type' => 'DATETIME',
    ),
];

$args_array['order'] = 'ASC';
$args_array['orderby'] = 'meta_value';

Also I need a field just for order (import_fields_order_by_start_date) to combine dates/times to be ‘Y-m-d H:i:s’ but as I fetch data from JSON and parse them into ACF group where date & time need to be separated in ACF field repeater:

repeater field

I need to combine date & start time..

repeater values combined and parsed to this field

So what I did is on save to parse values from repeater (first screenshot) to field which use case is just for order (second screenshot):

function update_event_order_date( $post_id ){

    if (get_post_type( $post_id ) == 'tplus_event') {

        $first_date = get_field( 'import_fields', $post_id )['eventsdates'][0];

        if( !empty( $first_date ) ) {

            $start_array = array( 'order' => array( 
                'by_start_date' => ($first_date['date'] . " " . $first_date['starttime']), 
                'by_end_date' => ($first_date['date'] . " " . $first_date['endtime']) 
                )
            );

            update_field('import_fields', $start_array, $post_id); 
            
        }

    }
}
add_action('acf/save_post', 'update_event_order_date'); 

The problem is, when I save the post, date is parsed in exactly right format, but that ‘post’ dissapears from final query result.. both in array & front.

Query SQL:

    [request] => 
                    SELECT SQL_CALC_FOUND_ROWS  VaKgVkKg_posts.ID
                    FROM VaKgVkKg_posts  INNER JOIN VaKgVkKg_postmeta ON ( VaKgVkKg_posts.ID = VaKgVkKg_postmeta.post_id )
                    WHERE 1=1  AND ( 
  ( VaKgVkKg_postmeta.meta_key = 'import_fields_order_by_start_date' AND CAST(VaKgVkKg_postmeta.meta_value AS DATETIME) >= '2022-12-15 15:25:07' )
) AND ((VaKgVkKg_posts.post_type="tplus_event" AND (VaKgVkKg_posts.post_status="publish" OR VaKgVkKg_posts.post_status="acf-disabled" 
OR VaKgVkKg_posts.post_status="private")))
                    GROUP BY VaKgVkKg_posts.ID
                    ORDER BY CAST(VaKgVkKg_postmeta.meta_value AS DATETIME) ASC
                    LIMIT 0, 8

Also to mention, I am parsing dates directly into ‘order’ field from JSON when fetching data, it works fine until I update the ‘post’.
When I comment the function which does “acf/save_post” and update the post, then it is OK..

Appreciate your help and sorry for my bad English.

0
Bruno 2 months 2022-12-15T09:31:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse