Modify the $args array which is used to query the posts shown in the relationship, post object, and page link fields. In this case, only include posts with the status of “publish”.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Include only published posts in post query. | |
* | |
* @param array $args | |
* @param array $field | |
* @param int $post_id | |
* @return array | |
*/ | |
function nn_query_only_published_posts( $args, $field, $post_id ) { | |
$args['post_status'] = array( 'publish' ); | |
return $args; | |
} | |
add_filter( 'acf/fields/relationship/query', 'nn_query_only_published_posts', 10, 3 ); | |
add_filter( 'acf/fields/post_object/query', 'nn_query_only_published_posts', 10, 3 ); | |
add_filter( 'acf/fields/page_link/query', 'nn_query_only_published_posts', 10, 3 ); |