Abundis
Abundis

Reputation: 11

Gatsby not fetching related posts from WordPress using gatsby-source-wordpress

I'm using Gatsby with a headless WordPress setup and WPGraphQL. I have a custom post type Talk, and within this post type, I am trying to retrieve related posts through a custom field relatedPosts.

While everything works fine in GraphiQL and Apollo Playground (the related posts are returned correctly), I'm encountering issues when trying to query them in Gatsby.

What I have: I am able to query Talk posts and related posts via WPGraphQL directly (using GraphiQL or Apollo Playground), and the response is correct.

Example query in WPGraphQL:

query {
  talks {
    nodes {
      title
      relatedPosts {
        title
      }
    }
  }
}

Response:

{
  "data": {
    "talks": {
      "nodes": [
        {
          "title": "Some talk title",
          "relatedPosts": [
            { "title": "Related Post 1" },
            { "title": "Related Post 2" }
          ]
        }
      ]
    }
  }
}

In Gatsby, I'm using gatsby-source-wordpress and have configured it properly in gatsby-config.js.

The issue: When querying for the same data in Gatsby using the following query in GraphiQL, the relatedPosts field returns null.

query {
  allWpTalk {
    nodes {
      title
      relatedPosts {
        title
      }
    }
  }
}

The query returns:

{
  "data": {
    "allWpTalk": {
      "nodes": [
        {
          "title": "Some talk title",
          "relatedPosts": null
        }
      ]
    }
  }
}

What I’ve tried so far: Confirmed Data Availability: Verified that relatedPosts is available in the WordPress GraphQL schema.

Schema Customization: Added a schema customization in gatsby-node.js to link relatedPosts to WordPress_Post

Gatsby Clean: Tried clearing Gatsby’s cache with gatsby clean and rebuilding with gatsby develop.

Verbose Logging: Enabled verbose mode in gatsby-source-wordpress for debugging, but didn’t see any errors or useful logs related to this issue.

PS.: This is the snippet that that expose GraphQL

       register_graphql_field(
            $graphql_type,
            'relatedPosts',
            [
                'type' => ['list_of' => 'Post'], // Ensure it matches the expected GraphQL type
                'description' => __('Related posts', 'hcpt'),
                'resolve' => function($post, $args, $context) {
                    // Retrieve related post IDs from meta
                    $related_ids = get_post_meta($post->ID, '_related_posts', true);
                    $related_ids = is_array($related_ids) ? array_map('intval', $related_ids) : array_map('intval', explode(',', $related_ids));
        
                    if (empty($related_ids)) {
                        return [];
                    }
        
                    // Fetch related posts
                    $related_posts = get_posts([
                        'post_type'   => ['post', 'code-projects', 'talks', 'articles', 'multimedia', 'media-appearances', 'thesis'],
                        'post__in'    => $related_ids,
                        'orderby'     => 'post__in',
                        'numberposts' => -1,
                    ]);
        
                    // Convert post IDs into GraphQL-compatible objects
                    return array_map(function($related_post) use ($context) {
                        return \WPGraphQL\Data\DataSource::resolve_post_object($related_post->ID, $context);
                    }, $related_posts);
                },
            ]
        );

Upvotes: 0

Views: 20

Answers (0)

Related Questions