Adds support to terms pagination
This commit is contained in:
parent
1964576d36
commit
afe2f7974c
|
@ -78,6 +78,7 @@ class REST_Controller extends \WP_REST_Controller {
|
|||
'metacompare' => 'meta_compare',
|
||||
'hideempty' => 'hide_empty',
|
||||
'perpage' => 'posts_per_page',
|
||||
'number' => 'number',
|
||||
'paged' => 'paged',
|
||||
'postin' => 'post__in',
|
||||
'relation' => 'relation',
|
||||
|
|
|
@ -192,7 +192,11 @@ class REST_Collections_Controller extends REST_Controller {
|
|||
|
||||
$total_items = wp_count_posts( $item->get_db_identifier(), 'readable' );
|
||||
|
||||
if (isset($total_items->publish)) {
|
||||
if (isset($total_items->publish) ||
|
||||
isset($total_items->private) ||
|
||||
isset($total_items->trash) ||
|
||||
isset($total_items->draft)) {
|
||||
|
||||
$item_arr['total_items']['trash'] = $total_items->trash;
|
||||
$item_arr['total_items']['publish'] = $total_items->publish;
|
||||
$item_arr['total_items']['draft'] = $total_items->draft;
|
||||
|
|
|
@ -284,6 +284,7 @@ class REST_Terms_Controller extends REST_Controller {
|
|||
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
|
||||
|
||||
$args = $this->prepare_filters($request);
|
||||
$prepared_args = $args;
|
||||
|
||||
$terms = $this->terms_repository->fetch($args, $taxonomy);
|
||||
|
||||
|
@ -292,7 +293,27 @@ class REST_Terms_Controller extends REST_Controller {
|
|||
array_push($response, $this->prepare_item_for_response( $term, $request ));
|
||||
}
|
||||
|
||||
return new \WP_REST_Response($response, 200);
|
||||
$response = new \WP_REST_Response($response, 200);
|
||||
|
||||
if(isset($args['number'], $args['offset'])){
|
||||
unset( $args['number'], $args['offset'] );
|
||||
$total_terms = wp_count_terms( $this->taxonomy->get_db_identifier(), $args );
|
||||
|
||||
if ( ! $total_terms ) {
|
||||
$total_terms = 0;
|
||||
}
|
||||
|
||||
$per_page = (int) $prepared_args['number'];
|
||||
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
|
||||
|
||||
$response->header( 'X-WP-Total', (int) $total_terms );
|
||||
|
||||
$max_pages = ceil( $total_terms / $per_page );
|
||||
|
||||
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue