Thursday, December 15, 2022
HomeWordPress Developmentmeta question - acf/save_post affecting WP_Query outcomes

meta question – acf/save_post affecting WP_Query outcomes


I’ve CPT Occasions, which lists from WP_Query and orders by meta question the place occasions which have date/time much less then present are usually not proven + ordered by most closest occasions to additional.

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 as a result of I have to separate args for occasions & posts:

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

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

Additionally I want a area only for order (import_fields_order_by_start_date) to mix dates/instances to be ‘Y-m-d H:i:s’ however as I fetch knowledge from JSON and parse them into ACF group the place date & time have to be separated in ACF area repeater:

repeater field

I want to mix date & begin time..

repeater values combined and parsed to this field

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

perform 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 issue is, once I save the submit, date is parsed in precisely proper format, however that ‘submit’ dissapears from remaining question consequence.. each in array & entrance.

Question 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="non-public")))
                    GROUP BY VaKgVkKg_posts.ID
                    ORDER BY CAST(VaKgVkKg_postmeta.meta_value AS DATETIME) ASC
                    LIMIT 0, 8

Additionally to say, I’m parsing dates straight into ‘order’ area from JSON when fetching knowledge, it really works fantastic till I replace the ‘submit’.
After I remark the perform which does “acf/save_post” and replace the submit, then it’s OK..

Respect your assist and sorry for my dangerous English.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments