Skip Navigation

[Resolved] Condition for custom field during post creation

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

Problem:

I'm trying to add 'event like' information to two different post types.

I want a "repeatable event" to happen any day of the week (every week), multiple times during the same day.

Solution:

I suggest you try with one-to-many relationship instead of Repeatable Field Group, for example:

https://toolset.com/forums/topic/condition-for-custom-field-during-post-creation/#post-2053671

Relevant Documentation:

https://toolset.com/glossary/post-relationships/

This support ticket is created 2 years, 11 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 5 replies, has 2 voices.

Last updated by diegoQ-2 2 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#2052387

Hello!
I'm trying to add 'event like' information to two different post types. I'm using the same custom field group for both types. While it is restricted (no repeatable groups), I can make it work.

Now, I'm creating a "repeatable event" (please note this is the name of the feature I want to store in the custom fields). I want a "repeatable event" to happen any day of the week (every week), multiple times during the same day.
To accomplish this, I have created a select field that includes the name of the days (Mon to Sun) to select when in the week the event happens (and will be repeated every week). Then I create a single field to store the time for each day (for instance, "time for Monday", "time for Tuesday", and so on), making time fields repeatable.
So far so good (with a repeatable group would be much easier but still). Now I want to add to each time field a display condition only to be shown if the correspondent day is marked in the select field, but the 'basic' condition creator does not show the checkboxes field, and I have no clue on how to create custom logic to accomplish such a thing.

Any idea on how to proceed to accomplish my intention, either by providing conditional logic or another approach that may be more efficient?

[Select Field] (if not marked, do not store anything on database)
	Title -> 'Mon', Value -> 1
	Title -> 'Tue', Value -> 2
	Title -> 'Wed', Value -> 3
	Title -> 'Thu', Value -> 4
	Title -> 'Fri', Value -> 5
	Title -> 'Sat', Value -> 6
	Title -> 'Sun', Value -> 7

[Time field] (repeatable)
	Title -> 'Time for Mon'
	'Display Condition' -> ( ??? = 1)

[Time field] (repeatable)
	Title -> 'Time for Tue'
	'Display Condition' -> ( ??? = 2)

[Time field] (repeatable)
	Title -> 'Time for Wed'
	'Display Condition' -> ( ??? = 3)

[Time field] (repeatable)
	Title -> 'Time for Thu'
	'Display Condition' -> ( ??? = 4)

[Time field] (repeatable)
	Title -> 'Time for Fri'
	'Display Condition' -> ( ??? = 5)

[Time field] (repeatable)
	Title -> 'Time for Sat'
	'Display Condition' -> ( ??? = 6)

[Time field] (repeatable)
	Title -> 'Time for Sun'
	'Display Condition' -> ( ??? = 7)

Notes:
+ Time field is a Date field that I will later only use the time information.
+ the "???" above is the logic I'm asking for.
+ I tried the approach here https://toolset.com/forums/topic/if-condition-for-a-custom-field-with-multiple-checkboxes/ didn't work.
+ I tried this logic and didn't work: ($(dias-de-semana)[0] eq 1)

Thanks!
Diego

#2052785

Hello,

The logic you mentioned above won't work, custom date field works for specific day + time, it won't be able to repeatable for every weekday.

In your case, I suggest you try with the a custom Repeatable Field Group, with below three custom select fields:
- Select field "Weekday", with below options:
Title -> 'Mon', Value -> 1
Title -> 'Tue', Value -> 2
Title -> 'Wed', Value -> 3
Title -> 'Thu', Value -> 4
Title -> 'Fri', Value -> 5
Title -> 'Sat', Value -> 6
Title -> 'Sun', Value -> 7
- Select field "Start time", with 24 options: 0~23
- Select field "End time", with 24 options: 0~23

So when you edit a post, you can add multiple events into the Repeatable Field Group, and select the "Weekday" + "Start time" + "End time" for each event.

More help:
https://toolset.com/course-lesson/creating-and-displaying-repeatable-field-groups/

#2053151

Hello Luo Yang, I like your approach much better. However, I face a problem. As I'm using this custom field group in two post types simultaneously, I'm not allowed to do a repeatable group.
Now, I'm using the same custom filed group in multiple post types because my site is about online and in-person courses. I'm using the LearnDash plugin for online training, but I'm using a custom post form Toolset for in-person courses. But I want to show all courses (online and in-person) in a schedule view, where users can filter by type (online and in-person, and many others) using GridBuilder WP.
As far as I know, I may have the same custom fields applying for both post types to show them both simultaneously with the option of filters.

If you have better ideas, please share.

#2053671

The Toolset Repeatable Field Group is based on one-to-many relationship, so you use one-to-many relationship instead of Repeatable Field Group, for example:
1) Create a custom post type "Event", with the custom fields I mentioned above:
https://toolset.com/forums/topic/condition-for-custom-field-during-post-creation/#post-2052785

2) Setup multiple one-to-many relationships between your custom post types and "Event"
https://toolset.com/glossary/post-relationships/

So when you edit custom post types post, you can setup related "Event" posts, and in single custom post types post, you can display the related "Event" posts:
https://toolset.com/course-lesson/displaying-related-posts/#displaying-many-related-items

#2054377

Hello Luo Yang, your approach is very clever, and it worked perfectly for me.
I still need to understand how to hide the new "event" post type from the site as it will only be used to store event-like information of the courses. I don't want to have an archive or individual pages of events visible on my site. I already "hide" them with Yoast, but that only works for search engines. I want to disable front-end interaction completely.
Is that possible?

#2054459

Under the post type configuration, I found out that if I disable the publicly_queryable option, the post type will not have a front-end view. Just what I needed.

My issue is resolved now. Thank you very much!!

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