Home › Forums › General Issues › Events Manager and ACF › Reply To: Events Manager and ACF
This plugin saves the end date in a custom field named ‘_end_ts’ which is a timestamp. Unfortunately, I don’t know of a safe way to filter this to your existing filter, but it is possible to filter the values saved in this field and to remove the unwanted relationships when loading the field value. This would have the added side benefit of automatically removing expired events the next time the post with the relationship field is edited, if you don’t want that then you can make it only happen in the front end
add_filter(
'acf/load_value/name=YOUR_FIELD_NAME_HERE',
'remove_expired_events_form_YOUR_FIELD_NAME_HERE',
10, 3
);
function remove_expired_events_form_YOUR_FIELD_NAME_HERE($value, $post_id, $field) {
// if you want to have this run in the admin
// remove this check
if (is_admin()) {
return $value;
}
if (!is_array($value) || !count($value)) {
// bail early, no related events
return $value;
}
$count = count($value);
$now = time();
for ($i=0; $i<$count; $i++) {
$expire = intval(get_post_meta($value[$i], '_end_ts', 0));
if ($expire < $now) {
unset($value[$i]);
}
}
return $value;
}
Your other choice would be to create a WP pre_get_posts filter that could do the same thing. https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts. This would probably work faster but I’m not sure if there’d be any side effects.
add_action('pre_get_posts', 'no_expired_events', 0);
function no_expired_events($query) {
if (is_admin()) {
return;
}
if ($query->query_vars['post_type'] == 'event') {
$meta_query = array(
array(
'key' => '_end_ts',
'value' => time(),
'compare' => '>',
'type' => 'TIME'
)
);
$query->set('meta_query', $meta_query);
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.