Tips / Recommendations

Sorting WordPress posts by creation time and AFC custom field

add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);

function my_pre_get_posts($query) {

    if ($query->is_main_query() && !is_admin()) { 
        $query->set('meta_key', 'sorting'); // сортировка по произвольному полю
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC'); // порядок сортировки
    }
    $query->set('meta_query', $meta_query);
}
function postSorting() {
    $todays_date = current_time('Ymd');
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => '4',
        'paged' => $paged,
        'meta_query' => array(
            'relation' => 'AND',
            'sorting_clause' => array(
                'key' => 'sorting',
                'type' => 'numeric',
                'value' => array(0, 100),
                'compare' => 'BETWEEN',
                
            ),
            'date' => array(
                'date_clause' => array(
                    'key' => 'date',
                    'compare' => '<=',
                    'value' => $todays_date,
                ),
                'time_clause' => array(
                    'key' => 'time',
                    'compare' => 'EXISTS',
                ),
            )
        ),
        'orderby' => array(
            'sorting_clause' => 'ASC',
            'date_clause' => 'ASC',
            'time_clause' => 'ASC',
        ),
    );
 
    $query = new WP_Query( $args );

    //вывод цикла...

    // сброс
    wp_reset_postdata();
}