Last active
May 11, 2023 15:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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