Skip Navigation

[Resolved] Query filter on empty checkbox

This support ticket is created 6 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#900854

I have a checkbox that saves 1 to db. (not 0 if not checked)

Now I am trying to filter a view to show only posts that does not equal 1, on this field.
I want to display all posts that have not checked this box.

Problem is that most existing posts have no post meta for this field so I only get a blank result in the view.
Tried string ne 1 and number ne 1.
Also tried lt 1.

#900873

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tina

This is the way WP Query works.

This is what the generated meta query looks like:

[meta_query] => Array
        (
            [0] => Array
                (
                    [key] => wpcf-yay-or-nay
                    [value] => 1
                    [type] => CHAR
                    [compare] => !=
                )

            [relation] => AND
        )

In words I'm asking WP to return posts where the wpcf-yay-or-nay field does not equal 1 (which is the value stored in the database when the field is checked).

WordPress will only return posts where the key exists and is not equal to 1.

So a solution would be to save 0 to the database if it is not checked, which would then work, but doesn't help if you already have lots of posts published that you would need to edit and re-save.

It wouldn't be too difficult to write a script that you run once (Code Snippets plugin is ideal for this) that loops over the posts, checks if they have the meta key present, and if not creates it with a value of 0.

Alternatively, you could modify the query using wpv_filter_query so that it returns posts where the meta value is not 1 whether it exists or not. This relies on a trick which is described in this SO answer: https://wordpress.stackexchange.com/a/81831/35739

#905063

I posted a long time ago that it would be great to get a 'Not Exists' option in the Views Query Filter. But It hasn't been implemented yet.
Any news on that?

(Ill go with the wpv_filter_query 🙂

#906122

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tina

I checked and the request is recorded but you are right it hasn't been implemented yet.

When our public request system goes online people will get a chance to vote on this and other issues.