Sunday, June 5, 2022
HomeWordPress Developmentwp question - How can I create 'future' and 'previous' parameter for...

wp question – How can I create ‘future’ and ‘previous’ parameter for restAPI by filtering the CPT customized date area by larger than / lower than present datetime?


add_filter( ‘rest_events_query’, ‘filter_events_by_date_field’, 999, 2 );
perform filter_events_by_date_field( $args, $request ) {
if ( ! isset( $request[‘filter_date’] ) ) {
return $args;
}

$filter_date = sanitize_text_field( $request['filter_date'] );
$datetime = current_time('mysql');
if( $filter_date == 'future' ) {
    $filter_date_meta_query = array(
        'key' => 'date',
        'worth' => $datetime,
        'kind' => 'DATETIME',
        'examine' => '>='
    );
} else {
    $filter_date_meta_query = array(
        'key' => 'date',
        'worth' => $datetime,
        'kind' => 'DATETIME',
        'examine' => '<'
    );
}


if ( isset( $args['meta_query'] ) ) {
    $args['meta_query']['relation'] = 'AND';
    $args['meta_query'][] = $filter_date_meta_query;
} else {
    $args['meta_query'] = array();
    $args['meta_query'][] = $filter_date_meta_query;
}

// $args['meta_key'] = 'date';
// $args['orderby'] = 'meta_value_num';
// $args['order'] = 'ASC';

return $args;

}

Sorry guys i’ve no coding expertise could be nice if somebody will help. thanks!!!!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments