Only Published Posts in ACF Relationship Fields
Limits the choices in ACF relationship and post object fields to published content, newest first, so editors cannot link to drafts.
Opens WPIDE on
Code
$wpide_acf_published_only = function ( $args ) {
$args['post_status'] = 'publish';
$args['orderby'] = 'date';
$args['order'] = 'DESC';
return $args;
};
add_filter( 'acf/fields/relationship/query', $wpide_acf_published_only );
add_filter( 'acf/fields/post_object/query', $wpide_acf_published_only );
Notes
Applies to every relationship and post object field; add /name=your_field to the hook names to target one.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.