Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created March 9, 2023 23:00
Show Gist options
  • Save jasonbahl/52930129a183d7ab63b038ce3eee33e2 to your computer and use it in GitHub Desktop.
Save jasonbahl/52930129a183d7ab63b038ce3eee33e2 to your computer and use it in GitHub Desktop.
add_filter( 'graphql_pre_resolve_field', function( $default, $source, $args, $context, $info, $type_name, $field_key, $field, $field_resolver ) {
// if the field is not the 'tags' field, and the
if ( ! ( 'tags' === $field_key && 'post' === strtolower( $type_name ) ) ) {
return $default;
}
$empty = [
'edges' => [],
'nodes' => [],
];
if ( $source instanceof \WPGraphQL\Model\Post && 'revision' === $source->post_type ) {
$terms = wp_get_object_terms( $source->databaseId, 'post_tag' );
if ( empty( $terms ) ) {
return $empty;
}
$term_ids = wp_list_pluck( $terms, 'term_id' );
$resolver = new \WPGraphQL\Data\Connection\TermObjectConnectionResolver( $source, $args, $context, $info );
$resolver->set_query_arg( 'include', $term_ids );
return $resolver->get_connection();
}
return $empty;
}, 10, 9 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment