Skip to content

Instantly share code, notes, and snippets.

@joshuafredrickson
Last active August 5, 2020 14:42
Show Gist options
  • Save joshuafredrickson/60e99978d31f14f488b7510b3ca10bc0 to your computer and use it in GitHub Desktop.
Save joshuafredrickson/60e99978d31f14f488b7510b3ca10bc0 to your computer and use it in GitHub Desktop.
wp-graphql.php
query NODE_LIST_QUERY($first: Int!, $after: String) {
posts(first: $first, after: $after, where: { parent: null }) {
nodes {
acfFeaturedImage {
fieldGroupName
scoutFeaturedImageOverride {
id
sourceUrl
}
scoutFeaturedImageShow
scoutFeaturedVideo
}
author {
node {
id
}
}
authorDatabaseId
authorId
categories(first: 100) {
nodes {
id
}
}
commentCount
commentStatus
content
contentType {
node {
id
}
}
databaseId
date
dateGmt
desiredSlug
enclosure
excerpt
featuredImage {
node {
id
sourceUrl
}
}
featuredImageDatabaseId
featuredImageId
guid
id
isRevision
lastEditedBy {
node {
id
}
}
link
menu_order
modified
modifiedGmt
pingStatus
pinged
postFormats(first: 100) {
nodes {
id
}
}
slug
status
template {
__typename
... on DefaultTemplate {
templateName
}
... on AboutTemplate {
templateName
}
... on BlogTemplate {
templateName
}
... on ConnectTemplate {
templateName
}
... on HomeTemplate {
templateName
}
... on LandingTemplate {
templateName
}
... on ServicesTemplate {
templateName
}
... on WorkTemplate {
templateName
}
}
terms(first: 100) {
nodes {
id
}
}
title
toPing
uri
__typename
}
pageInfo {
hasNextPage
endCursor
}
}
}
<?php
/**
* Posts
*/
$post_post_type_object = get_post_type_object('post');
// Primary Category
register_graphql_object_type('PrimaryCat', [
'description' => "The post's primary category as defined by The SEO Framework.",
'fields' => [
'title' => [
'type' => 'String',
'description' => 'Category title',
],
'uri' => [
'type' => 'String',
'description' => 'Category URI',
],
'databaseId' => [
'type' => 'Integer',
'description' => 'Category database ID',
],
'extra' => [
'type' => 'String',
'description' => 'For dev use only I suppose',
]
],
]);
register_graphql_field($post_post_type_object->graphql_single_name, 'PrimaryCat', [
'description' => "Get a post's primary category.",
'type' => 'PrimaryCat',
'resolve' => function ($post) {
$primary_term = the_seo_framework()->get_primary_term($post->ID, 'category');
if (! $primary_term) {
$terms = get_the_terms($post->ID, 'category');
$primary_term = $terms[0];
}
return [
'title' => $primary_term->name,
'uri' => str_replace(home_url(), '', get_term_link($primary_term)),
'databaseId' => $primary_term->term_id,
// 'extra' => json_encode($terms),
];
},
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment