Query WordPress REST API for pages with a specific template - markhowellsmead/helpers GitHub Wiki
This would also work with any meta key / value combination.
Frontend
https://DOMAIN/wp-json/wp/v2/pages?metaKey=_wp_page_template&metaValue=my_template1.php
…or multiple values (array)…
https://DOMAIN/wp-json/wp/v2/pages?metaKey=_wp_page_template&metaValue%5B0%5D=my_template1.php&metaValue%5B1%5D=my_template2.php
…or in the Block Editor environment…
import { store as coreDataStore } from '@wordpress/core-data';
const pages = useSelect(
select =>
select(coreDataStore).getEntityRecords('postType', 'page', {
metaKey: '_wp_page_template',
metaValue: [
'my_template1.php',
'my_template2.php',
],
}),
[]
);
Query modification on the server
function filter_rest_page_query($query_vars, $request)
{
switch ($request->get_param('metaKey')) {
case '_wp_page_template':
$query_vars['meta_key'] = '_wp_page_template';
$query_vars['meta_value'] = $request->get_param('metaValue');
break;
}
return $query_vars;
}
add_filter('rest_page_query', 'filter_rest_page_query', 10, 2);