Skip Navigation

[Resolved] Filter out posts without featured image

This support ticket is created 5 years, 4 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by nikolaS-3 5 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1346917

Tell us what you are trying to do?

Is it possible to set in view or wordpress archive view a filter that filters out posts from loop that do not have featured image?

#1346947

Hello,

It is possible with custom codes, you can try the views filter hook wpv_filter_query, for example, for a normal post view( ID 123), you can add below codes into your theme file 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 == 123) {
    $args = array(
      'relation' => 'AND',
      array(
        'key' => '_thumbnail_id',
        'compare' => 'NOT EXISTS'
      )
    );
    // add these arguments to your meta query
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }
  return $query;
}

Please replace 123 with your view's ID

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#1346989

Hi, thanks for the code!

I've added these lines in functions.php and replaced ID with my wordpress archive view id.

Still, it's displaying posts without featured image. Here is a link: hidden link

#1347003
Screenshot 2019-09-24 at 11.54.54.png

Hmm, still nothing has changed.

I'm sending functions.php SC in attachment

#1347051

I assume we are talking about the "apartment" term's archive page

For the WordPress archive, you will need to try WordPress built-in filter hook "pre_get_posts", for example, you can change the PHP codes as below:

add_action( 'pre_get_posts', 'add_filter_by_thumbnail', 99);
function add_filter_by_thumbnail ( $query ) {
  if( ! is_admin() && $query->is_main_query() && is_tax('property-type', 'apartment') ) {
    $args = array(
      'relation' => 'AND',
      array(
        'key' => '_thumbnail_id',
        'compare' => 'EXISTS'
      )
    );
	$meta_query = (array)$query->get('meta_query');
    $meta_query[] = $args;
	$query->set( 'meta_query', $meta_query );
  }
  return $query;
}

More help:
https://developer.wordpress.org/reference/hooks/pre_get_posts/

#1347057

Nice! It works 😀

Is it possible to apply this code to all "property-type" archives? or I need to add this code for each property type category?

For istance, other than apartment I have hotels, rooms, camping, etc. And also translation of them.

#1347061

You can change this code from:
is_tax('property-type', 'apartment')

To:
is_tax('property-type')

More help:
https://codex.wordpress.org/Function_Reference/is_tax

#1347063

My issue is resolved now. Thank you!