Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Last active May 11, 2023 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbahl/0bbc22c6cae959b54fa8d7c0a54a3abf to your computer and use it in GitHub Desktop.
Save jasonbahl/0bbc22c6cae959b54fa8d7c0a54a3abf to your computer and use it in GitHub Desktop.
Override a resolver for wp-graphql-acf v0.6.1 and older to prevent orphaned IDs from causing errors.
function _graphql_acf_sanitize_post_object_resolver( $value ) {
if ( ! $value instanceof \WPGraphQL\Model\Post ) {
return $value;
}
if ( 'publish' !== $value->status ) {
return $value;
}
}
add_filter( 'graphql_resolve_field', function( $default, $source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info, $type_name, $field_key, $field, $field_resolver ) {
if ( ! isset( $info->fieldDefinition->config['acf_field']['type'] ) ) {
return $default;
}
if ( 'post_object' !== $info->fieldDefinition->config['acf_field']['type'] ) {
return $default;
}
if ( empty( $default ) ) {
return $default;
}
if ( is_array( $default ) ) {
return array_filter( array_map( '_graphql_acf_sanitize_post_object_resolver', $default ) );
}
return _graphql_acf_sanitize_post_object_resolver( $default );
}, 10, 9 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment