Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Post Object field and Relationship field return null for arrays. #155

Closed
HumidBrains opened this issue Jul 14, 2020 · 4 comments · May be fixed by #297
Closed

Post Object field and Relationship field return null for arrays. #155

HumidBrains opened this issue Jul 14, 2020 · 4 comments · May be fixed by #297

Comments

@HumidBrains
Copy link

I’m trying to relate products from within a Flexible Content field in ACF. That works when querying a single product with ACF Post Object by setting “Select multiple values?” to No.
However, when querying for multiple values, instead of an array of product objects, I get null in WPGraphiQL.
Same happens if using ACF Relationship Field, which I think always returns an array. The result is the same for simple and variable products.

This should be easy to replicate as it happens to me however I define my field groups

Using
WPGraphQL 0.10.2
wp-graphql-woocommerce 0.5.1
wp-graphql-acf 0.3.4

@HumidBrains
Copy link
Author

@jasonbahl Will there be any love given to this one anytime soon? :)

@gustavarrhenius
Copy link

@HumidBrains @jasonbahl
I discovered the same problem in our project, and it seems like its the graphql_acf_post_object_source filter that wp-graphql-woocommerce uses and returns wrong value.

File includes/class-acf-schema-filters.php line 56.

public static function resolve_post_object_source( $source, $value ) {
	$post = get_post( $value );
	if ( $post instanceof \WP_Post ) {
		switch ( $post->post_type ) {
			case 'shop_coupon':
				$source = new Coupon( $post->ID );
				break;
			case 'shop_order':
				$source = new Order( $post->ID );
				break;
			case 'product':
				$source = new Product( $post->ID );
				break;
		}
	}

	return $source;
}

Since the Post Object field can return both null and an array this code wont work because when querying a single product get_post returns itself, and the code has no support for an array.

@vstrelianyi
Copy link

vstrelianyi commented Oct 13, 2021

having the same issue

versions:
WP 5.8.1
Woo 5.8.0
Graphql 1.6.5
WPGraphQL WooCommerce 0.10.4
Advanced Custom Fields PRO 5.10.2
WPGraphQL for Advanced Custom Fields 0.5.3

Steps:

I'm having a CPT - Auctions
Auction has an ACF Relationship field with Products
when I'm running the following query - I'm receiving an error

QUERY
image

ERROR
image

@jasonbahl
Copy link
Contributor

This is a pretty old issue, but it typically occurred when there were orphaned IDs stored as values for ACF Fields.

WPGraphQL for ACF has been re-built over here: https://github.com/wp-graphql/wpgraphql-acf and relational fields are all now using connections and WPGraphQL DataLoaders which account for issues like orphaned IDs not throwing errors.

I recommend switching to the new version of WPGraphQL for ACF at your earliest convenience as it's the version we'll be releasing on WordPress.org and supporting long-term.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.