Wordpress JSON api fetch selected items -
how can fetch post title, excerpt of posts of wordpress blog using json api.
currently, using https://www.example.com/wp-json/wp/v2/posts, returns lot of data slows down whole process. there url can fetch selected fields?
have considered writing own api endpoints? https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
something work endpoint becomes http://example.com/wp-json/myplugin/v1/post-titles:
function my_awesome_func( $data ) { $args = array( 'post_type' => 'post', ); $query = new wp_query( $args ); $arr = array(); while ( $query->have_posts() ) { $query->the_post(); $titles = get_the_title(); array_push($arr, $titles); } return $arr; } add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/post-titles', array( 'methods' => 'get', 'callback' => 'my_awesome_func', ) ); } );
Comments
Post a Comment