register_rest_field query parameter code example

Example: register_rest_field query parameter

public function __construct() {  
    // Make sure you add those numbers at the end of the line, or it won't work
    add_filter( 'rest_opps_query', array( $this, 'get_opps_by_state' ), 10, 2 );  
}  
       
public function get_opps_by_state( $args, $request ) {

    $state = $request->get_param( 'state' );  

    if( $state == 'open' ) {
        $args[ 'meta_query' ] = array(
            'deadline' => array(
                'key' => 'deadline',
                'value' => date( 'Ymd' ), 
                'compare' => '>',
                'type' => 'DATE'
            )
        );
    } elseif ( $state == 'closed' ) {
        $args[ 'meta_query' ] = array(
            'deadline' => array(
                'key' => 'deadline',
                'value' => date( 'Ymd' ), 
                'compare' => '<',
                'type' => 'DATE'
            )
        );
    }

    return $args; 

}

Tags:

Misc Example