Friday, October 7, 2022
HomeWordPress Developmentheadless - YARRP associated posts with customized put up kind and wp-graphql

headless – YARRP associated posts with customized put up kind and wp-graphql


I am utilizing YARPP, WPGraphQL and YARPP WPGraphQL (which is a plugin to attach these two).

Think about I’ve two put up sorts: Put up (default) and guide (customized put up kind). For each guide, I need to get 5 comparable Put ups and 5 comparable guides (whole 10 posts). The issue is it does not return equal worth of every. For some guides I get 8 comparable books and a couple of posts, for an additional one I get 6 books and 4 posts. The full is at all times 10, but it surely’s not at all times 5 + 5.

question NewQuery {
  books {
    nodes {
      title
      relatedBooks {
        nodes {
          title
        }
      }
      relatedPosts {
        nodes {
          title
        }
      }
      
    }
  }
}


Plugin code (modified model of https://github.com/matepaiva/yarpp-wpgraphql):

add_action('graphql_register_types', operate () {
  international $yarpp;
  if ($yarpp) {
    register_graphql_connection([
      'fromType' => 'book',
      'fromFieldName' => 'relatedPosts',
      'toType' => 'post',
      'connectionTypeName' => 'BookRelatedPosts',
      'connectionArgs' => [
        'limit' => [
          'name' => 'limit',
          'type' => 'Int',
          'description' => 'Override's YARPP setting's "Maximum number of related posts." The maximum number is 20.'
        ]
      ],
      'resolve' => operate ($put up, $args, $context, $data) {
        international $yarpp;
        $restrict = isset($args['where']['limit']) ? $args['where']['limit'] : null;
        $related_posts = $yarpp->get_related($post->ID, $restrict ? ['limit' => $limit] : null);
        $args['where']['in'] = array_map(operate ($related_post) {
          return $related_post->ID;
        }, $related_posts);

        $resolver = new WPGraphQLDataConnectionPostObjectConnectionResolver(null, $args, $context, $data, 'put up');
        $consequence = $resolver->get_connection();
        return $consequence;
      }
    ]);
  }
});

add_action('graphql_register_types', operate () {
    international $yarpp;
    if ($yarpp) {
      register_graphql_connection([
        'fromType' => 'book',
        'fromFieldName' => 'relatedBooks',
        'toType' => 'book',
        'connectionTypeName' => 'BookRelatedBooks',
        'connectionArgs' => [
          'limit' => [
            'name' => 'limit',
            'type' => 'Int',
            'description' => 'Override's YARPP setting's "Maximum number of related posts." The maximum number is 20.'
          ]
        ],
        'resolve' => operate ($put up, $args, $context, $data) {
          international $yarpp;
          $restrict = isset($args['where']['limit']) ? $args['where']['limit'] : null;
          $related_posts = $yarpp->get_related($post->ID, $restrict ? ['limit' => $limit] : null);
          $args['where']['in'] = array_map(operate ($related_post) {
            return $related_post->ID;
          }, $related_posts);
  
          $resolver = new WPGraphQLDataConnectionPostObjectConnectionResolver(null, $args, $context, $data, 'guide');
          $consequence = $resolver->get_connection();
          return $consequence;
        }
      ]);
    }
  });

YARRP choices:

The Pool:
    put up sorts to incorporate: Posts, Books
The Algorithm:
    match threshold: 1
    titles, our bodies, classes, tags: think about
Automated Show Choices: (I am on a headless atmosphere, however most variety of posts impacts the question)
    most variety of posts: 10

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments