Created
April 25, 2018 04:04
-
-
Save rahularyan/5e98244578d3162d670e84b463845dfd to your computer and use it in GitHub Desktop.
Get AnsPress questions using WP REST api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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