Skip Navigation

[Resolved] Query Filter by Featured Image

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

Problem: I would like to add a Query Filter that checks whether or not a post has a featured image.

Solution: WordPress stores the featured image in the postmeta table under the key "_thumbnail_id". Using the Views API wpv_filter_query, you could add a check for that meta key to the meta query already generated by the Query Filters. Here's a snippet you can add to functions.php:

[php]
add_filter( 'wpv_filter_query', 'add_filter_by_thumbnail', 10, 3 );
function add_filter_by_thumbnail ( $query, $view_settings, $view_id ) {
if( $view_id == 12345 ) {
$args = array(
'relation' => 'AND',
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
)
);
// add these arguments to your meta query
$query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
$query['meta_query'][] = $args;
}
return $query;
}
Replace 12345 with the numeric ID of this View, and you should find that the results of the View all have featured images defined.

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

0% of people find this useful.

This support ticket is created 6 years, 6 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 3 voices.

Last updated by Christian Cox 4 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#868283

th

Tell us what you are trying to do? I want to create a View of custom post types by using the featured image as query filter.
In Edit View > Query Filter > Add a filter > Post filters >

Can I set the query filter so that "Show the posts that contain a featured image. Do not show posts that do not have a featured image." I could not find the filter for such a thing. Please see the screenshot.

Is there a similar example that we can see?
See this screenshot: hidden link
See this screenshot - List of filters available, but not featured image: hidden link

#868533

Hi, there is currently no way to add a Featured Image filter from wp-admin, and the only way I can think to add one is to use custom code and the wpv_filter_query API. WordPress stores the featured image in the postmeta table under the key "_thumbnail_id", so you could add a check for that meta key to the meta query already generated by the Query Filters. Here's a snippet you can add to functions.php:

add_filter( 'wpv_filter_query', 'add_filter_by_thumbnail', 10, 3 );
function add_filter_by_thumbnail ( $query, $view_settings, $view_id ) {
  if( $view_id == 12345 ) {
    $args = array(
      'relation' => 'AND',
      array(
        'key' => '_thumbnail_id',
        'compare' => 'EXISTS'
      )
    );
    // add these arguments to your meta query
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }
  return $query;
}

Replace 12345 with the numeric ID of this View, and you should find that the results of the View all have featured images defined.

#871134

th

Thank you. I have copied and pasted the code above to functions.php of the child theme and changed the View ID. However, I am still not seeing the changes I desire. I am double checking that the View ID is correct (the said View contains the query filter) and I am also testing on a separate test website. I will update on this at a later time.

#873031

I will mark this ticket as pending an update from you. No need to reply right now, the ticket will remain open for 30 days.

#878814

th

Thank you for the reply. The code worked on the test site.

On my live site, I have removed another code from /functions.php and the filter query finally worked.

This is the code that I removed (hidden link), and this might have caused a clash.

#1399563

This should just be a selectable filter instead of custom code.

#1399729

Hi adrianM-6, if you have not already done so I encourage you to submit your request to add the featured image filter here: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
This will let our management team know you want to see the feature added to a future release of the software.