Support

Account

Home Forums General Issues Remove current post/page from relationship field

Solved

Remove current post/page from relationship field

  • Is this possible? So it doesn’t create a loop?

  • 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

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Remove current post/page from relationship field’ is closed to new replies.