Home › Forums › General Issues › Remove current post/page from relationship field
Hi @roflman79
Sure, you can use the following filter to modify the query args!
http://www.advancedcustomfields.com/resources/filters/acf-fields-relationship-query/
Thanks
E
Hi Elliot, it’s probably my fault, but I am not able to exclude the current post from the field. I have a very strange issue here, here is is my code:
add_filter('acf/fields/relationship/query', 'exclude_id', 10, 3);
function exclude_id ($args) {
$current_id = $_GET['post'];
$args['post__not_in'] = array($current_id);
return $args;
}
This doesn’t work, but if I hardcode the id (e.g. $args[‘post__not_in’] = array( 115 ); ) I obtain the exclusion.
I tried to substitute $_GET with:
$current_id = (int) $_GET['post'];
$current_id = intval($_GET['post']);
global $post;
$current_id = $post->ID;
But I had no luck..
Hi @roflman79
The issue is that your functions only allows for 1 parameter ($args).
This is not correct. It should instead allow for all 3 parameters.
Please see the documentation for more info.
Thanks
E
Working version of the code from @roflman79.
/**
* Exclude current post/page from relationship field results
*/
// 1. Add the name=[NAME_OF_RELATIONSHIP_FIELD].
add_filter('acf/fields/relationship/query/name=[NAME_OF_REALTIONSHIP_FIELD]', 'exclude_id', 10, 3);
// 2. Add the $field and $post arguments.
function exclude_id ( $args, $field, $post ) {
//3. $post argument passed in from the query hook is the $post->ID.
$args['post__not_in'] = array( $post );
return $args;
}
This is for disable the current post
//I got the result when i change $post to $post->ID//
//3. $post argument passed in from the query hook is the $post->ID.
$args[‘post__not_in’] = array( $post->ID );
Thanks Jamie
The topic ‘Remove current post/page from relationship field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.