Skip to content

Instantly share code, notes, and snippets.

@mvaessen
mvaessen / WP REST-API search all (specified) post-types
Last active Aug 4, 2016
In order to search all post-types using the WP REST-API, which is not possible with the API, you can use this piece of code to search for all specified post-types. Replace all the 'POSTTYPE' parts with your CPT's slug, and it will be searchable in 1 query. You can also add additional CPT's. Using the ?embedded=true, you get all the embedded data!
View WP REST-API search all (specified) post-types
<?php
add_action('rest_api_init', function () {
register_rest_route('mvaessen/v1', '/search', array(
'methods' => 'GET',
'callback' => 'search_all_endpoint',
));
});
function search_all_endpoint(WP_REST_Request $request)