Last active
September 13, 2018 05:43
-
-
Save izzygld/c5d5b7ca67ffa4b0d8bbcee390652cce to your computer and use it in GitHub Desktop.
acf gallery images resolver
This file contains hidden or 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
add_filter('graphql_post_fields', function($fields) { | |
$fields['post_images'] = [ | |
'type' => \WPGraphQL\Types::list_of(\WPGraphQL\Types::post_object('attachment')), | |
'description' => __('Images on posts', 'mostly plants'), | |
'resolve' => function(\WP_Post $post) { | |
$post_images = get_field('post_images', $post->ID); | |
$images = []; | |
if (!empty($post_images) && is_array($post_images)) { | |
for ($i = 0; $i < count($post_images); $i++) { | |
$images[] = \WPGraphQL\Data\DataSource::resolve_post_object($post_images[$i]['ID'], 'attachment'); | |
} | |
} | |
return $images; | |
}, | |
]; | |
return $fields; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment