Support

Account

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