Skip Navigation

[Resolved] View query filter for whether an image custom field has content

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

Problem:

The issue here is that the user wanted to filter their view with a query filter to not include posts where the image custom field is empty.

Solution:

This can be done with the hook below.

  //Return only  products with banner images
add_filter( 'wpv_filter_query', 'filter_empty_banners', 99, 3 );
  
function filter_empty_banners( $query_args,$view_settings ,$view_id ) {
     
    if ( $view_id == 44576) {
        $query_args['meta_query'] = array(
          array(
            'key' => 'wpcf-banner',
            'value' => '',
            'compare' => '!='
            )
          );
    }
    return $query_args;
}

Add the above to your Toolset custom code section in Toolset -> Custom Code and activate it. You will need to change the 44576 to your view's id as well as the wpcf-banner to the slug of your image field keeping the wpcf- prefix.

This support ticket is created 3 years, 8 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Anthony 3 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1984071

Tell us what you are trying to do? I am building a 'slider' view using an image custom field and want to skip posts where that field is empty.

What is the link to your site? hidden link

#1984371

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Anthony,

Thank you for getting in touch.

IN a case like this I would recommend that you use the conditional block to check if the image field is not empty. This means that the items for that post won't display unless the specified field is not empty.

For a better overview of the block conditional I would recommend taking a look at our documentation below.

https://toolset.com/course-lesson/using-toolset-conditional-block/

Please let me know if this helps.

Thanks,
Shane

#1984419

I was dubious that using a conditional as you suggest but tried it anyway and indeed it did not resolve the issue. Let me be more descriptive of the situation: I'm building a 'slider' View using the WooCommerce Product CTP for content. The images to be displayed by the slider are in an image custom field "Banner" in a custom field group attached to the Product CPT. Not all products have content in the "Banner" custom field. When viewing the slider on the front end, when it reaches a product without a Banner, the content normally beneath the slider moves up and then moves down again when the next product with a banner takes its place. So I want to use a query filter to skip products where the Banner image field is empty...

#1984439

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Anthony,

Checking on this the conditional should've only allowed posts with the images populated to appear, however I can see where the issue lies in that this is being paginated. Essentially the empty items are still being added to the view output, just not being displayed.

A better solution for this would be to use the filter hook for views to ensure that views only retrieves the posts that have the field filled in. Add the following to the custom code section in Toolset -> Settings -> Custom code and ensure that it is activated.

  //Return only  products with banner images
add_filter( 'wpv_filter_query', 'filter_empty_banners', 99, 3 );
 
function filter_empty_banners( $query_args,$view_settings ,$view_id ) {
    
    if ( $view_id == 44576) {
        $query_args['meta_query'] = array(
          array(
            'key' => 'wpcf-banner',
            'value' => '',
            'compare' => '!='
            )
          );
    }
    return $query_args;
}

This should now produce the desired results.

Thanks,
Shane

#1984455

My issue is resolved now. Thank you!