[Resolved] How to customize search results page to only show pages with a featured image

Home Forums Support [Resolved] How to customize search results page to only show pages with a featured image

Home Forums Support How to customize search results page to only show pages with a featured image

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2337838
    Jin

    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.

    #2337960
    David
    Staff
    Customer Support

    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/

    #2338575
    Jin

    Thanks David appreciate it.

    #2338805
    David
    Staff
    Customer Support

    You’re welcome

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.