Skip Navigation

[Resolved] Using Checkbox from CPT in view does not filter as expected

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

Problem:
How to display all records where custom checkbox field is not checked.

Solution:
To fetch records where postmeta key is not exists, Please try to add following code to your current theme's functions.php file.

add_filter( 'wpv_filter_query', 'func_not_checked_posts', 10, 3 );
function func_not_checked_posts( $query_args, $view_settings, $view_id ) {
    if($view_id = 999999) {
        $query_args['meta_query'][] = array(
            'key'     => 'wpcf-interested',
            'compare' => 'NOT EXISTS',
        );
    }
    return $query_args;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 7 years, 1 month 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
- 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 Erik 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#579816

Hi, I've created a CPT "Participants".
These Participants show correctly in a View
hidden link click on Parcipants Tab please.

Now my client wants an extra list next to Participants named "Interested" where he will show the people who are not sure yet if they want to participate.

I added a checkbox field to CPT "Participants" named "Interested". When you click on it the value in the database will become 1.
Then I made a new View named Interested and added it as a tab in the Template.
Added a filter for the checkbox. If number is equal to 1 : show the interested only.
This all works great!

Now I want to add a filter to the Participants list not showing the interested people. But I cannot get this to work.
I tried: if checkbox "Intereseted" : If number is not equal to 1 : don't show in the list.
Or: if number is equal to 0
Or: with the names option

Is there a way how I can setup the filter that it will not show the Interested in the Participants list?

Thank you
Erik

#579822

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The filter you added should not be working because there must not be postmeta key exists with value 0 in postmeta table for such posts.

To fetch records where postmeta key is not exists, Please try to add following code to your current theme's functions.php file.

add_filter( 'wpv_filter_query', 'func_not_checked_posts', 10, 3 );
function func_not_checked_posts( $query_args, $view_settings, $view_id ) {
    if($view_id = 999999) {
        $query_args['meta_query'][] = array(
            'key'     => 'wpcf-interested',
            'compare' => 'NOT EXISTS',
        );
    }
    return $query_args;
}

Where:
- Replace 999999 with your original view ID

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

#579950

Dear Minesh,
You're the best. Perfect solution!

Thanks,
Erik