Skip Navigation

[Resolved] Creating a view of posts with a featured image

This support ticket is created 2 years, 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by saulB-3 2 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2233411
screenshot 2021-11-30 at 11.07.42.png

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

#2233877

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

#2234491

This worked beautifully. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.