I am trying to make the wordpress search just display the pages which have the custom template that called "video-information.php".
In the head of the video-information.php file I added this to create a name to the template
<?php
/*
* Template Name: Video Information
*
* @package skytube
* @since skytube v1.0.0
*/
get_header();
?>
I know that wordpress search for the content and everything in the site but I want to limit the query to make it just looking after the pages which have a custom template.
I made some searches and found tutorials but all them talk just about how to limit the search query to the posts and pages or a custom post type. This is an example of those tutorials.
// In function.php file
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('post','page'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
The above code can just limit the search query to pages and posts not for specific page which have a custom template.
you can use meta_query and make something like this:
function search_custom_template($query) {
if ($query->is_search && !is_admin()) {
$query->set('meta_query', [['key' => '_wp_page_template', 'value' => 'template-custom.php']]);
}
return $query;
}
Check out the official docs: https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters