Hi there,
I have a custom post type called Artwork. In addition to having several custom fields associated with it, it also has the standard Featured Image (post thumbnail) field associated with it.
Not all Artwork posts will have a Featured Image. I'm trying to create a view that retrieves a list of 5 random Artwork posts that have a non-empty Featured Image field. I tried to create a filter on the view, but Featured Image isn't one of the fields that appears to be filterable (see attached). Is there a way to accomplish this?
Thanks!
Saul

Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
There is no GUI filter available to filter the posts by featured image. But we can workaround by adding a view's filter "wpv_filter_query":
For example - please try to add the following code to the "Custom Code" section offered by Toolset;
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
add_filter( 'wpv_filter_query', 'func_add_filter_by_featured_image', 10, 3 );
function func_add_filter_by_featured_image( $query, $view_settings, $view_id ) {
if( $view_id == 99999) {
$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;
}
Where:
- Replace 99999 with your original view ID.
More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
This worked beautifully. Thank you!