Support

Account

Home Forums Backend Issues (wp-admin) Relationship field user filter

Solved

Relationship field user filter

  • Hi there,

    i have 2 custom post types A and B.
    In A (admin) all values of B are selectable using relationship field.

    Now i want to show only the values of B, which were created by the current logged in user. This applies to the wp backend.

    *edit: Values of B should only be filtered for specified roles, an admin should see all values of B

    Thanks in advance!

  • 
    // this will effect all relationship fields
    // if you only want to effect specific fields
    // see https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
      add_filter('acf/fields/relationship/query', 'relationship_only_own_posts', 10, 3);
      function relationship_only_own_posts($args, $field, $post_id) {
        // update core is something only available to admin
        // or super admin on mutlisite
        // a lot less code than looking through roles
        if (current_user_can('update_core')) {
          // not on admin or editor
          return $args;
        }
        $author = get_current_user_id();
        $args['author'] = $author;
        return $args;
      }
    
  • Thank you! Exactly what i was looking for.

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

You must be logged in to reply to this topic.