Skip Navigation

[Resolved] Trouble Filtering on a Checkbox

This support ticket is created 2 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.

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)

This topic contains 4 replies, has 3 voices.

Last updated by aaronM-9 2 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#2183965

I am trying to create a view that displays and searches content from three different post types: posts, courses, and lessons. Posts may contain a custom field called "superseded" that has a 1 if checked and 0 if unchecked (although I've found it doesn't always record a zero if unchecked). Courses and lessons do NOT have the custom field "superseded".

I would like the view to only display content that does NOT have a superseded=1 value. It SHOULD display the content if the superseded value is either zero or non-existent. However, any filter whatsoever I try to put on eliminates all records from the output. Any help would be appreciated. Thanks.

- Aaron

#2184465

Nigel
Supporter

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

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

>It SHOULD display the content if the superseded value is either zero or non-existent.

The problem here is with "non-existant".

Under the hood Views queries the database via the core WP_Query class, and when you add a custom field filter the results that will be returned must have a value for the custom field (which then matches the specified condition, in this case not equal 1).

(More accurately, the custom field must have an entry in wp_postmeta for the post, even if the value is empty.)

So, for your posts to be shown you should make the custom field available to each of the post types, make sure that it is set to save 0 to the database if unchecked, and edit and save the posts so that they update wp_postmeta to include an entry for the custom field.

Or you could use the Views API to modify the query arguments, via wpv_filter_query (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query).

Check the documentation for WP_Query about how to use the meta_query parameter where you could check for whether the key NOT EXISTS or was not equal to 1.

https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

#2184779

It seems the easiest solution is to make the superseded field available to the other post types. However, there is one wrinkle in the plan. It is currently part of a field group and I don't want to make all the other fields also available to the other post types. Is there a way to migrate the custom field from one field group to another without fouling up the data in the posts? Thanks.

- Aaron

#2184903

Shane
Supporter

Languages: English (English )

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

Hi Aaron,

There is actually the option to re-use previously created custom fields.

What you can do is to go to the field group that is assigned to another post type then add a new field. From there you should see the button "Choose from previously created fields."

Once you click the button it should bring up all your existing custom fields for you to add it to the field group.

Thanks,
Shane

#2187013

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.