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
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.
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.
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.
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.
This should just be a selectable filter instead of custom code.
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.