Skip Navigation

[Resolved] Two more questions: Filter with checkbox field and What is better practice

This support ticket is created 5 years, 5 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Lee 5 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1316823

Lee

I've gotten basic views I needed to display. But in my "Performers" CPT, I have the "Active" checkbox field. Its configured as default to save with value "1" with checked.

Now in my view I've created without any filters it displays and formats the CPT data properly. But I would like it to only display the CPT records that are "Active". I create a Query Filter with Custom Field Filter, "Select items with field: Active Performer is a number equal to 1".

But no results are returned. Suggestions?

Second question. What is better practice? I have two similar fields for Performers CPT, "Active" and "Featured". Which is better? Create as a custom field in the CPT or CPT Taxonomies?

Thanks,

#1316847

Hello,

Q1) But no results are returned. Suggestions?
Yes, it is expected result.
Since you are querying the post type "Performance", but the custom field "Active" is in another post type "Performers", there isn't field "Active" in post type "Performance", so it conducts the problem:
But no results are returned

Views is using WordPress class WP_Query to query the posts, if you filter the posts by a custom field, those posts must have the specific custom field.

You might consider to move the field "Active" into post type "Performance"

Q2) Create as a custom field in the CPT or CPT Taxonomies?
In this case, I suggest try with custom checkbox field, since there is only two values in it: 1 or empty.

If there are multiple options, and you will need to add more options in future, you can consider custom taxonomies.

For your reference.

#1317501

Lee

I want to create a table schedule similar to this. I can produce this view, until I try and filter by the performers that are "Active". Then no results are returned. When I have "Performers" CPT selected as the view to display. If I select any of the other CPTs then I can't properly sort by times.

Times Stage Performer
10:00am - 1l:50am Main Stage David Marsh
10:15am - 11:00 Cultural Center Irish Dancers

This is my CPT configurations.

Three CPTs

Performers - the performers/events in festival
Locations - the locations/stages of performances
Performances - includes the start/end times of the performances

Taxonomies
Performer Categories - two checkbox fields; active, featured

Relationships (one to many)
Performers ->> Performers (performer-performance)
Locations ->> Performers (location-performance)

Suggestions on how to get a view like the above and filter on only the Performer "active" taxonomy. This is still early in project I would be willing to change how my CPTs are created if needed.

#1317523

Thanks for the details, as I mentioned above, there isn't such kind of built-in feature within Toolset Views plugin.

Currently, you can try custom codes, for example:
1) Create a post view "my-view", query "performance" posts

2) Use Views filter hook wpv_filter_query to trigger a PHP function
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

3) In this PHP function, do these:
a) Get all "Performers" post IDs which is assigned with term "active":
https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

b) Use above "Performers" post IDs to get all related "performance" posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

c) Use above "performance" post IDs to add a filter into the post view "my-view"
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
post__in (array) – use post ids. Specify posts to retrieve.

For your reference.

#1323671

Lee

I decided it best to split the "featured" as a checkbox field on both CPTs Performers and Performances. As in my case their are both featured performers and featured performances. Two seperate queries and displays. Thanks for your assistance.