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.
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.