I am creating a calendar which uses a Custom Post Type "Calendar Events" which contains an Repeatable Field Group "Cal Event Datetime" that contains two fields, EventDate, and EventTime.
I have two separate views:
1) Displays Cal Event Datetimes where EventDate is after Today(), and corresponding info (title/image/etc) from each parent Calendar Event post;
and 2) Display Calendar Events filtered by a category search, and using a nested view, display all the child CalEventDatetimes.
#1 can not filter by category, or any of the fields of the parent posts. #2 can not filter by the EventDates (i.e. remove events that have already happened)
Is there a way that I can have one single view which will accept a date AND a category (and/or other fields in the Calendar Event CPT)? For example, show only events with the category "Music" that occur after today?
Link to site: fxbgdev.wpengine.com
View #1: hidden link
View #2: hidden link
I've been banging my head on the wall for quite some time on this, so any guidance will be greatly appreciated!
Hello,
Toolset Repeatable Field Groups are based on one-to-many relationship, each item of Repeatable Field Groups is also a single post. When you query the parent "Calendar Events" posts, you can not filter the results by fields of another post type "Cal Event Datetime".
In your case, you might consider these:
1) Create two post types:
- Calendar Events
- Cal Event Datetime (with custom fields EventDate and EventTime)
2) Register taxonomy "category" into post type "Cal Event Datetime"
3) Setup one-to-many relationship between above post types "Calendar Events" and "Cal Event Datetime"
4) Create a post view:
- Query "Cal Event Datetime" posts,
- Filter by:
a) taxonomy "category"
b) fields EventDate
- In view's loop, display the parent "Calendar Events" post information + "Cal Event Datetime" post information:
https://toolset.com/course-lesson/displaying-related-posts/#displaying-one-related-item-parent
We tried this route, which brought about some more questions:
I created a CPT CalEventDateTime, linked by a one-to-many relationship to a parent CalendarEvent.
1) When entering a new CalEventDateTime, it requires a title, which we don't need. I see this being confusing to the end user, given it's a superfluous field. Is there any way to eliminate the requirement for a title? (screenshot 1) Eventually, there will be a form on the front end for the public to input events.
2) We need the ability to search by category, however the CalEventDateTimes don't have the parent's categories attached to them, the parent Calendar Event does. Is there a way to either force the child CalEventDateTime to automatically take the categories of the parent CalendarEvent?; Or allow my view to search/filter the categories of the parent CalendarEvent? Requiring the user to manually add the categories to both the event and each instance of CalEventDateTime seems unnecessarily inconvenient.
The second screenshot is an example of the functionality we are trying to replicate.
No, there isn't such kind of built-in feature within Toolset plugins, you can consider custom codes.
For example, see below test site:
hidden link
1) Restore back to the repeatable field groups "Cal Event Datetime" setting
hidden link
2) Enable legacy editor:
hidden link
Dashboard-> Toolset-> Settings-> General, in section "Editing experience", enable option:
Show both the legacy and Blocks interface and let me choose which to use for each item I build
3) Create a post view with custom search form:
hidden link
- Query "Calendar Events" posts
- Filter by:
a) Taxonomy "Category"
b) Custom date field
- In view's loop, display "Calendar Events" post information + Child "Cal Event Datetime" posts information
4) Dashboard-> Toolset-> Settings-> Custom codes:
hidden link
Add one item, with below codes:
add_filter('wpv_filter_query', function($query, $setting, $view_id){
if($view_id == 39){ // specific view ID
$rfg_slug = 'cal-event-datetime'; // Repeatable field group slug
$date_field_slug = 'wpcf-event-dt'; // custom date field slug
$date_url_param = 'wpv-wpcf-event-dt'; // date field URL parameter name
if( isset($_GET[$date_url_param]) && is_numeric($_GET[$date_url_param]) ){
foreach( $query['meta_query'] as $k => $v ){
if($v['key'] == $date_field_slug){
$date_meta_query = $v;
unset($query['meta_query'][$k]); // remove default datetime filter
}
}
$args = array(
'post_type' => $rfg_slug,
'nopaging' => true,
'meta_query' => array($date_meta_query),
'fields' => 'ids',
);
$Datetime_IDs = get_posts($args);
foreach($Datetime_IDs as $id){
$event_id = toolset_get_related_post($id, $rfg_slug); // Repeatable field group slug
$query['post__in'][] = $event_id;
}
}
}
return $query;
}, 99, 3);
Test it in frontend:
hidden link
More helps:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Yes! This looks like it will do what we need. Would it be possible to also add a keyword search? Would I need custom functionality for that, or would I just use the built-in filter?
Yes, you can use the built-in keyword search filter, it can works on "Calendar Events" post type.