Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active June 23, 2021 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/c76767f6cfd38c13d9004cd8417ef4e2 to your computer and use it in GitHub Desktop.
Save kellenmace/c76767f6cfd38c13d9004cd8417ef4e2 to your computer and use it in GitHub Desktop.
<?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