• Skip to content
  • Skip to primary sidebar

Nate Nault

Code & other things

  • CONTACT

Query Only Published Posts in ACF Relational Fields

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”.


/**
* 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 );

view raw

acf.php

hosted with ❤ by GitHub

Advanced Custom Fields

Copyright © 2025 Nate Nault