Skip Navigation

[Resolved] Cannot filter non-existent boolean fields from view blocks

This support ticket is created 4 years, 2 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 1 reply, has 2 voices.

Last updated by Shane 4 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1778011

I am trying to:

I have a checkbox called "hide" as a custom field on a post type called "listing". This checkbox has been configured to put a "1" in the database when it is checked and to put nothing in the database when it is not checked. When I use the toolset view block to show a set of "listing" posts, I want it to show only those that do not have a value "1" in the "hide" field.

I try to do this by asking for "hide is a string different from 1" or "hide is a string not in 1". None of this works. It seems that if the post does not have a "hide" field at all (because nothing is stored in the db when not checked), then it cannot be part of any custom field filter.

Another case is a set of people with a "deceased" date. Those who have died get a date, those who have not died do not have a date. But I cannot figure out how to show only those who are alive, in other words, those without any deceased date.

I expected to see:

I expected to see an option like for "exists", so that I could say "hide" is "present" or is "absent" to make my filter. Similarly I could only include those where "deceased" is "absent" to get everyone alive listed.

Instead, I got:

The "different from" do not treat non-existent fields as "different from" the value. If a field does not exist, you could certainly consider it "different from" the field value.

The "not in" does not treat a non-existent field as "not in" a set of values. If a field does not exist, then it certainly is "not in" that set of values.

Something needs to be done so that "non-existence" can be tested for in these filters.

#1779051

Shane
Supporter

Languages: English (English )

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

Hi Tseng,

In this case you will need to use a hook to filter out the posts that don't have this field in the database.

Here is an example that I had before.

add_filter( 'wpv_filter_query', 'display_not_exist_posts', 99, 3 );
function display_not_exist_posts( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 123 ) { // if it is specific view and by default
        $query_args['meta_query'][] = array(
                'key'     => 'wpcf-customfieldslug', 
                   'compare' => 'NOT EXISTS',
        );
    }
    return $query_args;
}

What you need to do is to change the 123 to the ID of your view and the "wpcf-customfieldslug" to the slug of your custom field keeping the wpcf- prefix.

Add this custom code to your Toolset Custom Code section in Toolset -> Settings -> Custom Code and activate it.

Please let me know if this helps.
Thanks,
Shane