0

I want to hide posts without featured images from index pages, can someone please give me a function code for it or if there is a plugin that does this then please point me to it.

Thanks in Advance. M.C.

1 Answer 1

2

You can manipulate the main WP_Query just before it asks the database for posts. Looking for an existing _thumbnail_id meta field should suffice, so using the pre_get_posts filter, something like this might work for you:

add_action("pre_get_posts", function($query) {
    if(!is_front_page() || !$query->is_main_query()) { 
        return;
    }
    if(!array_key_exists("meta_query", $query->query_vars)) {
        $query->query_vars["meta_query"] = array();
    }
    array_push($query->query_vars["meta_query"], 
        array(
        'key' => '_thumbnail_id',
        'compare' => 'EXISTS'
        )
    );
});

The conditional return at the start of the function is there to only target the main query on the front page, but leave alone any other queries (so it doesn't hide your posts without thumbnails from the archives, for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.