Last active
June 23, 2021 20:29
-
-
Save kellenmace/c76767f6cfd38c13d9004cd8417ef4e2 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
// Get all tag url paths for sitemap. | |
add_action('graphql_register_types', function () { | |
register_graphql_field('RootQuery', 'getTagUrlPaths', [ | |
'type' => ['list_of' => 'String'], | |
'args' => [ | |
'pageNo' => [ | |
'type' => 'Int', | |
'description' => __('Page number', 'text-domain'), | |
], | |
'perPage' => [ | |
'type' => 'Int', | |
'description' => __('Number of tag URLs per page', 'text-domain'), | |
], | |
], | |
'description' => __('This function returns tag urls, It takes pageNo and PerPage as optional args.', 'text-domain'), | |
'resolve' => function ($source, $args, $context, $info): array { | |
$paged = $args['pageNo'] ?? 1; | |
$per_page = $args['perPage'] ?? 10; | |
$offset = ($paged - 1) * $per_page; | |
$number = $per_page + $offset; | |
$tag_ids = get_tags([ | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'number' => $number, | |
'offset' => $offset, | |
'fields' => 'ids', | |
]); | |
$home_url = home_url(); | |
$tag_urls = array_map(fn ($tag_id) => get_tag_link($tag_id), $tag_ids); | |
$tag_url_paths = array_map(fn ($tag_url) => str_replace($home_url, '', $tag_url), $tag_urls); | |
return $tag_url_paths; | |
}, | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment