0

I am converting a WordPress website to use Next.js with wp-graphql. The site has a gallery category, and the gallery posts contain multiple images. I need to query the gallery items using wp-graphql, I can't use ACF.

I tried to register a gallery field

function wpgraphql_get_post_gallery($source, $args, $context, $info) {
    $post_slug = $source['slug'];
    $post = get_page_by_path($post_slug, OBJECT, 'post');
    if (!$post) {
        return null;
    }
    $gallery = get_post_gallery( $post->ID, false );
    if ( empty( $gallery ) ) {
        return null;
    }
    $ids = explode(',', $gallery['ids']);
    return array_map( 'intval', $ids );
}

add_action( 'graphql_register_types', function() {
    register_graphql_field( 'Post', 'gallery', [
        'type' => ['list_of' => 'Int'],
        'resolve' => 'wpgraphql_get_post_gallery',
    ] );
} );
ilyes
  • 1

0 Answers0