Skip Navigation

[Resolved] query filter by “has featured image” on view?

This thread is resolved. Here is a description of the problem and solution.

Problem:
I would like to add a query filter to a view that shows only posts that have a featured image.

Solution:
This can be achieved by using our views custom filters. Add this code in your theme’s or child theme’s functions.php file:

add_filter('wpv_filter_query', 'show_featured', 10, 3);
function show_featured($query, $view_settings,$view_id ) {
    if($view_id == 999) //replace view ID
    {
        $query['meta_key'] = '_thumbnail_id';
    }
    return $query;
 }

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Kacey 7 years ago.

Assisted by: Noman.

Author
Posts
#584662

I would like to add a query filter to a view that shows only posts that have a featured image. However, it's not a field listed in the query filter options. Suggestions for how to make this happen?

#584739

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Allison,

Thank you for contacting Toolset support. This can be achieved by using our views custom filters. Add this code in your theme’s or child theme’s functions.php file:

add_filter('wpv_filter_query', 'show_featured', 10, 3);
function show_featured($query, $view_settings,$view_id ) {
    if($view_id == 999) //replace view ID
    {
        $query['meta_key'] = '_thumbnail_id';
    }
    return $query;
 }

In above code, replace 999 with your View ID.

Thank you

#584834

thank you!