wp-graphql / wpgraphql-acf

Re-architecture of WPGraphQL for ACF
GNU General Public License v3.0
83 stars 12 forks source link

User ACF field only returns a value if it matches the post author. #234

Closed missionmike closed 1 month ago

missionmike commented 1 month ago

Description

When I set a value for a Users ACF field, sometimes the user value is not returned via GraphQL.

Example below showing:

image

However, the REST response does include the user value:

image

When I change the post author to "User Two", I can see the expected result in WPGraphQL response (see Post Author value in right side of screenshot):

image

This issue also affects the ACF Users field when "Select Multiple" is enabled on the field. WPGraphQL will only return one user if it's the post author. Otherwise, the result is empty.

Returns user when user is the same as post author:

image

Returns empty list when none of users are the post author:

image

However, REST response will consistently return all ACF Users values:

image

Steps to reproduce

Use this repository to access a quick minimal reproduction: https://github.com/missionmike/wordpress-minimal-repro-wpgraphql-acf-multiple-users

Note: this repo is based on a boilerplate I set up here, which is a work in progress. Please let me know if you run into any issues! The goal is to make minimal repro setup fast and easy within VS Code.

Steps to reproduce are included in the README, but I will paste the general steps here:

  1. Set up an ACF field for "Users" and add to posts.
  2. Assign a user to this field in a test post. Ensure that this user is NOT the same user as the post author.
  3. Fetch the WPGraphQL response for this post. The ACF Users value is empty.
  4. Change the post author to match the ACF user (or vice-versa).
  5. Refetch with WPGraphQL and you should see the correct user in the ACF User field.

PHP or JSON export of the ACF Field Group(s)

group_66be57e0d401c.json.zip

Additional context

No response

WPGraphQL Version

1.28.0

WPGraphQL For ACF Version

2.4.1

ACF (Advanced Custom Fields) Version. Free or Pro?

6.3.5 Free

WordPress Version

6.6.1

PHP Version

8.2.22

Additional enviornment details

No response

Please confirm that you have searched existing issues in the repo.

Please confirm that you have disabled ALL plugins except for WPGraphQL, WPGraphQL For ACF, ACF, etc.

missionmike commented 1 month ago

I plan to dive into this to learn a bit more about how this works under the hood. If I make progress perhaps I can open a PR to patch.

If anyone could point me to a place in the code that handles this functionality, it could give me a head start.

Thanks to everybody involved for working on these plugins!

josephfusco commented 1 month ago

Hey @missionmike, I believe that this is related to users not being public by default, until they author a published post. You can read more about that here https://www.wpgraphql.com/docs/users#sensitive-data.

We also have a post about how to make users public https://www.wpgraphql.com/2020/12/11/allowing-wpgraphql-to-show-unpublished-authors-in-user-queries

Hoping this helps! If you believe that this could be a different issue, feel free to add more details below.

missionmike commented 1 month ago

@josephfusco wow! I searched all over and didn't stumble across this article. This worked perfectly. Thank you so much.

I tested by adding these hooks to functions.php in my minimal repro, as stated in the article you shared:

add_filter( 'graphql_connection_query_args', function( $query_args, $connection_resolver ) {

    if ( $connection_resolver instanceof \WPGraphQL\Data\Connection\UserConnectionResolver ) {
      unset( $query_args['has_published_posts'] );
    }

    return $query_args;

}, 10, 2 );

add_filter( 'graphql_object_visibility', function( $visibility, $model_name, $data, $owner, $current_user ) {

    // only apply our adjustments to the UserObject Model
    if ( 'UserObject' === $model_name ) {
      $visibility = 'public';
    }

    return $visibility;

}, 10, 5 );

Results in Postman using "select multiple" on the post that has "admin" as the author, and using two author users in ACF without published posts:

image

Sorry I didn't find this earlier, but perhaps this issue's SEO will help others who stumble across the same issue in the future.

Thanks so much for getting back to me! Seems like this can be closed so I'll close it out.