Skip to content

Instantly share code, notes, and snippets.

@rahularyan
Created April 25, 2018 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahularyan/5e98244578d3162d670e84b463845dfd to your computer and use it in GitHub Desktop.
Save rahularyan/5e98244578d3162d670e84b463845dfd to your computer and use it in GitHub Desktop.
Get AnsPress questions using WP REST api
<?php
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* The version 1 endpoint for AnsPress questions.
*/
class My_REST_Questions {
/**
* Generate results for the /wp-json/anspress/v1/questions route.
*
* @param \WP_REST_Request $request Full details about the request.
* @return \WP_REST_Response|\WP_Error The response for the request.
*/
public static function get_items( \WP_REST_Request $request ) {
$params = $request->get_params();
$query = ap_get_questions(
array(
'showposts' => 20,
)
);
$questions = [];
while ( $query->have_posts() ) :
$query->the_post();
$questions[] = get_post();
endwhile;
$resp = rest_ensure_response( $questions );
$resp->header( 'X-WP-Total', (int) $query->found_posts );
$resp->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
return $resp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment