acf/fields/post_object/query

Overview

This hook allows you to modify the $args array which is used to query the posts shown in the the post_object select field.

Changelog

  • 5.0.0 – Changed 3rd parameter from $post to $post_id
  • ACF version 4.1.2 or above

Parameters

filter( $args, $field, $post_id )
  • $args (array) the WP_Query args used to find choices
  • $field (array) the field array containing all attributes & settings
  • $post_id (int) the current post ID being edited

Usage

There are 3 ways to hook into this filter

  1. acf/fields/post_object/query – filter for any field
  2. acf/fields/post_object/query/name={$field_name} – filter for a specific field based on it’s name
  3. acf/fields/post_object/query/key={$field_key} – filter for a specific field based on it’s key

functions.php

<?php

function my_post_object_query( $args, $field, $post_id ) {
	
    // only show children of the current post being edited
    $args['post_parent'] = $post_id;
	
	
	// return
    return $args;
    
}


// filter for every field
add_filter('acf/fields/post_object/query', 'my_post_object_query', 10, 3);


// filter for a specific field based on it's name
//add_filter('acf/fields/post_object/query/name=my_post_object', 'my_post_object_query', 10, 3);


// filter for a specific field based on it's key
//add_filter('acf/fields/post_object/query/key=field_508a263b40457', 'my_post_object_query', 10, 3);

?>