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

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.