When using the relationship ACF field, this snippet will only show the published items of the specified CPT.
In this example, the CPT is ‘Services’ and ‘services_list’ is the name of the ACF Relationship field.
$options
uses the WP_Query arguments used to specify the post status we want.
function cpt_services_relationship_filter( $options ) {
$options['post_status'] = array( 'publish' );
return $options;
}
add_filter( 'acf/fields/relationship/query/name=services_list', 'cpt_services_relationship_filter' );
Additional Reading:
- ACF Docs: Relationship
- ACF Docs: Relationship Query
if it’s possible to exclude all post without a thumbnail?
Good question! Yup, you can query for anything with the usual WP_Query parameters – Here’s a snippet exhibiting how.