Archived topic
How to customize search results page to only show pages with a featured image
3 replies · Started by Jin on September 9, 2022
I've tried to look for this and have read posts like this that talk about this problem, but only for Woocommerce.
I'd like to do two things:
1) Have the search results page only show pages (and not posts)
2) Have the search results page only show pages that have a featured image
Appreciate the the help. Thanks.
Hi there,
try adding this PHP Snippet:
function search_results_query( $query ) {
if ( $query->is_search && !is_admin() ) {
$query->set('post_type',array('page'));
$meta_query = array(
array(
'key'=>'_thumbnail_id',
'compare'=>'EXISTS',
),
);
$query->set('meta_query',$meta_query);
}
}
add_filter('pre_get_posts','search_results_query');
How to add PHP: https://docs.generatepress.com/article/adding-php/
Thanks David appreciate it.
You're welcome