Home › Forums › ACF PRO › How do I exclude posts older than a year from relationship field? › Reply To: How do I exclude posts older than a year from relationship field?
I figured this out for anyone who might come across this same requirement.
Simply use $args['date_query']
.
function acf_relationship_filter( $args, $field, $post_id ) {
$args['post_status'] = array('publish');
$args['date_query'] = array(
'after' => date('Y-m-d G:i:s', strtotime('-1 year')),
'inclusive' => true
);
return $args;
}
add_filter('acf/fields/relationship/query/name=featured_items', 'acf_relationship_filter', 10, 3);
This will only show the posts that were made after a year ago from today.
Hope it helps someone out there!
WP Docs on date parameters for WP_Query: http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
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.