#StackBounty: #php #wordpress #wp-graphql WordPress get_tags() not working with Wp-graphql plugin

Bounty: 50

I’m trying to get tag urls with pagination. This is the full code. I’m trying to extend the wp-grapphql function. This way i’ll be able to get tag paginated tag urls. I have morethan 30k tags so. I was able to sucessfully get paginated categories,post,author urls but dont know why this get_tags() not working as its suppose to in wp-graphql.

//Get all tag urls for sitemap
add_action('graphql_register_types', function () {
    register_graphql_field('RootQuery', 'getTagUrls', [
        'type' => ['list_of' => 'String'],
        'args' => [
            'pageNo' => [
                'type' => 'int',
            ],
            'perPage' => [
                'type' => 'int',
            ],
        ],
        'description' => __('This function returns tag urls, It takes pageNo and PerPage as optional args.'),
        'resolve' => function ($source, $args, $context, $info) {
            $tagUrls = array();
            $paged = (isset($args['pageNo'])) ? ($args['pageNo']) : 1;
            $perPage = (isset($args['perPage'])) ? ($args['perPage']) : 10;
            $offset = ($paged - 1) * $perPage;
            $number = $perPage + $offset;
            $tags = get_tags(array(
                'orderby' => 'name',
                'order' => 'ASC',
                'number' => $number,
                'offset' => $offset,
            ));
            foreach ($tags as $tag) {
                $fullUrl = esc_url(get_category_link($tag->term_id));
                $url = str_replace(home_url(), '', $fullUrl);
                array_push($tagUrls, $url);
            }
            return array_merge($tagUrls);
        },
    ]);
});


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.