In the next beta, Beta 13, you’ll be able to use before and after parameters for post collection queries:
wp-json/wp/v2/posts?after=2016-02-01T05:00:28
If you don’t want to wait until the next beta is released, you can clone or download the develop branch from Github.
Thread Starter
bgnjqw
(@bgnjqw)
Hi, I have try the develop branch, the after worked!
but it seemed that the before and after depend on date(with timezone), if I can use after and before on date_gmt.
but it seemed that the before and after depend on date(with timezone), if I can use after and before on date_gmt.
Right. before and after uses the date field, not date_gmt.
Thread Starter
bgnjqw
(@bgnjqw)
I do not know the wordpress instance timezone and I just have a gmt time, can I get posts by date_gmt field? or any workaround?
Thanks
You can choose a different column to run the query against with filter[date_query[column]]:
wp-json/wp/v2/posts?after=2016-02-01T05:00:28&filter[date_query[column]]=post_date_gmt
This is exactly what I’m looking to do using Version 2.0-beta15 with a custom post type that inherits from the WP_REST_Posts_Controller, but sorts based on an acf field. Unfortunately this isn’t working for me. 🙁
I’m hitting the following endpoint:
/wp-json/wp/v2/almanac_entry?per_page=3&filter[orderby]=acf_almanac_date&after=2016-12-23T00:00:00&filter[date_query[column]]=acf_almanac_date
and it’s returning three results (should be two) where two are after the date listed, and the third is before the date listed.
Here’s how I’m registering my actions:
add_action( 'init', 'register_custom_post_types', 25 );
function register_custom_post_types() {
global $wp_post_types;
$post_type_name = 'almanac_entry';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
add_action( 'rest_api_init', 'wp_rest_add_custom_fields' );
function wp_rest_add_custom_fields() {
register_rest_field('almanac_entry', 'acf_almanac_date', array (
'get_callback' => function($object, $field_name, $request) {
return get_post_meta( $object[ 'id' ], 'almanac_date', true );
},
'update_callback' => null,
'schema' => null,
));
}
I’m new to doing WP dev, so I may have done this wrong. I’m wondering if maybe it’s due to the priority not being set in add_action?
Any help is much appreciated.
-
This reply was modified 9 years, 6 months ago by
bbabics. Reason: trying to figure out how to format code in my post