Hi!
First of all thanks for you hard work, this is really awesome! 馃憤
I have encountered a bug/problem... I want to query with graphql an acf custom field gallery from a custom post type but the query contains only a boolean and no img sources.
On wp side it must be good, because in json I can find the attributes of each images so it must be something wrong when gatsby fetches the data probably. With single img it works perfectly so till I can fix it I'm using 3 fields.
The problem is that in graphql editor it doesn't see any attributes of the gallery type.
Thank you for your help in advance! :)
Hi @kepi0809 - this usually happens because wordpress return false
instead of empty array or null in case there are no data for some posts (you propably could check if that's the case by manually examining wordpress REST endpoint - something like http://your-url/wp-json/wp/v2/name-of-custom-type
) and this make gatsby confused because field is once array and once boolean.
If this is the case - then You would need to add following code snippet that force to return null instead of any empty values (like false
or empty arrays) to your wordpress instance - either via theme functions.php or via custom plugin. I didn't used gallery fields, so you might need to adjust filter name that target gallery - but image and relationship works fine for me.
function nullify_empty($value, $post_id, $field)
{
if (empty($value)) {
return null;
}
return $value;
}
add_filter('acf/format_value/type=image', 'nullify_empty', 100, 3);
add_filter('acf/format_value/type=relationship', 'nullify_empty', 100, 3);
// not sure if gallery is internally named gallery as well but this should work
add_filter('acf/format_value/type=gallery', 'nullify_empty', 100, 3);
Yeas thank you, one of the instances had indeed empty gallery. 馃憤
@kepi0809 Can we close this issue or you still experience this issue?
Just in case anyone else is searching, @pieh鈥檚 solution also fixed the problem I was having related to #3009 with repeaters:
add_filter('acf/format_value/type=repeater', 'nullify_empty', 100, 3);
Just on a sidenote from the the ACF docs.
Renamed from acf/format_value_for_api in v5.0.0
So in my functions.php in my hosted WP instance I changed the filter to:
add_filter('acf/format_value_for_api/type=image', 'nullify_empty', 100, 3);
Thank you, guys! It is working :)
Nailed it @pieh ! Thank you. Your suggested addition to my theme's functions.php has solved my repeatable image fields not working.
My error was Field "gallery_image" must not have a selection since type "Boolean" has no subfields
. This error is gone, GraphQL is querying repeatable image fields correctly and my Gatsby site is displaying images correctly.
Using Advanced Custom Fields PRO 5.5.3 and this solution worked for me:
// Nullifying Empty ACF Values
// Fixes optional fields being obscured by GraphQL
function nullify_empty($value, $post_id, $field)
{
if (empty($value)) {
return null;
}
return $value;
}
add_filter('acf/format_value/type=image', 'nullify_empty', 100, 3);
add_filter('acf/format_value/type=relationship', 'nullify_empty', 100, 3);
add_filter('acf/format_value/type=repeater', 'nullify_empty', 100, 3);
add_filter('acf/format_value/type=gallery', 'nullify_empty', 100, 3);
Thanks so much @pieh @kennethormandy !
So I was also having some trouble with empty text fields, was able to resolve by filtering all fields and returning an empty string vs null:
function nullify_empty( $value, $post_id, $field ) {
if( empty( $value ) ) {
return '';
}
return $value;
}
add_filter( 'acf/format_value', 'nullify_empty', 100, 3 );
Is there a way to implement this nullify_empty function with the acf object?
Sometimes my custom posts don't have any acf fields and so the build process crashes. I used @kmcaloon's solution to no avail.
Cannot query field "acf" on type "wordpress__thought_piece_endpoint_thought_pieces"
Most helpful comment
Hi @kepi0809 - this usually happens because wordpress return
false
instead of empty array or null in case there are no data for some posts (you propably could check if that's the case by manually examining wordpress REST endpoint - something likehttp://your-url/wp-json/wp/v2/name-of-custom-type
) and this make gatsby confused because field is once array and once boolean.If this is the case - then You would need to add following code snippet that force to return null instead of any empty values (like
false
or empty arrays) to your wordpress instance - either via theme functions.php or via custom plugin. I didn't used gallery fields, so you might need to adjust filter name that target gallery - but image and relationship works fine for me.