acf/fields/relationship/query

Overview

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

Changelog

  • 5.0.0 – Changed 3rd parameter from $post to $post_id
  • 4.1.2 – Added filter

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/relationship/query – filter for any field
  2. acf/fields/relationship/query/name={$field_name} – filter for a specific field based on it’s name
  3. acf/fields/relationship/query/key={$field_key} – filter for a specific field based on it’s key

functions.php

<?php

function my_relationship_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/relationship/query', 'my_relationship_query', 10, 3);


// filter for a specific field based on it's name
//add_filter('acf/fields/relationship/query/name=my_relationship', 'my_relationship_query', 10, 3);


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

?>

Related

Hi all. Server maintenance scheduled this Sunday 28th May. Learn more