Last active
March 20, 2023 14:27
-
-
Save L422Y/291db113194b69c7b3dd774423fdab22 to your computer and use it in GitHub Desktop.
WPGraphQL Custom Resolver: The Events Calendar PRO
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
{ | |
events(first: 10, where: {status: PUBLISH}) { | |
edges { | |
node { | |
uri | |
title | |
cost | |
costMax | |
costMin | |
content | |
organizers { | |
nodes { | |
phone | |
title | |
website | |
} | |
} | |
startDates | |
tags { | |
nodes { | |
name | |
slug | |
} | |
} | |
timezone | |
url | |
venue { | |
address | |
city | |
state | |
zip | |
link | |
} | |
phone | |
featured | |
featuredImage { | |
node { | |
medium: sourceUrl(size: MEDIUM) | |
large: sourceUrl(size: MEDIUM) | |
altText | |
} | |
} | |
eventId | |
eventsCategories { | |
nodes { | |
slug | |
name | |
} | |
} | |
endDate | |
duration | |
date | |
allDay | |
} | |
} | |
} | |
} |
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 | |
add_action('graphql_register_types', 'register_events_type'); | |
function register_events_type() | |
{ | |
register_graphql_object_type('CalendarEvent', [ | |
'description' => __("Calendar Event", 'your-textdomain'), | |
'fields' => [ | |
'databaseId' => [ | |
'type' => 'Int', | |
'description' => __('Database ID', 'your-textdomain'), | |
], | |
'title' => [ | |
'type' => 'String', | |
'description' => __('Title', 'your-textdomain'), | |
], | |
'excerpt' => [ | |
'type' => 'String', | |
'description' => __('Content', 'your-textdomain'), | |
], | |
'content' => [ | |
'type' => 'String', | |
'description' => __('Content', 'your-textdomain'), | |
], | |
'startDate' => [ | |
'type' => 'String', | |
'description' => __('Start Date', 'your-textdomain'), | |
], | |
'endDate' => [ | |
'type' => 'String', | |
'description' => __('End Date', 'your-textdomain'), | |
], | |
'city' => [ | |
'type' => 'String', | |
'description' => __('Event City', 'your-textdomain'), | |
], | |
'state' => [ | |
'type' => 'String', | |
'description' => __('Event State', 'your-textdomain'), | |
], | |
'address' => [ | |
'type' => 'String', | |
'description' => __('Event Address', 'your-textdomain'), | |
], | |
'cost' => [ | |
'type' => 'String', | |
'description' => __('Event Cost', 'your-textdomain'), | |
], | |
'country' => [ | |
'type' => 'String', | |
'description' => __('Event Country', 'your-textdomain'), | |
], | |
'eventLink' => [ | |
'type' => 'String', | |
'description' => __('Event Link', 'your-textdomain'), | |
], | |
'organizerLink' => [ | |
'type' => 'String', | |
'description' => __('Organizer Link', 'your-textdomain'), | |
], | |
'featuredImage' => [ | |
'type' => 'String', | |
'description' => __('Featured Image', 'your-textdomain'), | |
], | |
'other' => [ | |
'type' => 'String', | |
'description' => __('Other', 'your-textdomain'), | |
], | |
], | |
]); | |
} | |
add_action('graphql_register_types', 'register_events_field'); | |
function register_events_field() | |
{ | |
register_graphql_field('RootQuery', 'getCalendarEvent', [ | |
'description' => __('Get an event', 'your-textdomain'), | |
'type' => 'CalendarEvent', | |
'resolve' => function () { | |
return [ | |
'databaseId' => 4, | |
'title' => 'My Custom Event', | |
'content' => 'Lorem Ipsum', | |
'excerpt' => 'Lorem Ipsum', | |
'startDate' => '1983-01-25', | |
'endDate' => '2023-01-25', | |
"city" => "City", | |
"state" => "State", | |
"address" => "1234 street street", | |
"cost" => "69.99", | |
"country" => "USA", | |
"featuredImage" => "https://127.0.0.1/test.jpg", | |
"eventLink" => "https://www.google.com", | |
"organizerLink" => "https://www.google.com", | |
'other' => "", | |
]; | |
} | |
]); | |
register_graphql_field('RootQuery', 'upcomingEvents', [ | |
'description' => __('Upcoming Events', 'your-textdomain'), | |
'type' => ['list_of' => 'CalendarEvent'], | |
'resolve' => function () { | |
$query = new WP_Query(array( | |
'post_type' => 'tribe_events',//Display only event post types | |
'eventDisplay' => 'custom',//Needed to override tribe's modifications to the WP_Query | |
'order' => 'ASC',//Order events by the ones closest to today first | |
'orderby' => '_EventStartDate',//Order events using their start date | |
'meta_query' => array(array( | |
'key' => '_EventStartDate',//Compare using the event's start date | |
'value' => date('Y-m-d H:i:s'),//Compare against today's date | |
'compare' => '>=',//Get events that are set to the value's date or in the future | |
'type' => 'DATE'//This is a date query | |
)) | |
)); | |
$events = []; | |
foreach ($query->posts as $e) { | |
$event = [ | |
"databaseId" => $e->ID, | |
"title" => ($e->post_title), | |
"content" => ($e->post_content), | |
"excerpt" => (get_the_excerpt($e)), | |
"startDate" => tribe_get_start_date($e, true, 'Y-m-d H:i:s'), | |
"endDate" => tribe_get_end_date($e, true, 'Y-m-d H:i:s'), | |
"city" => (tribe_get_city($e)), | |
"state" => (tribe_get_state($e)), | |
"address" => (tribe_get_full_address($e)), | |
"cost" => (tribe_get_cost($e)), | |
"country" => (tribe_get_country($e)), | |
"eventLink" => (tribe_get_event_link($e)), | |
"organizerLink" => (tribe_get_organizer_website_url($e)), | |
"featuredImage" => (tribe_event_featured_image($e, 'medium', false, false)), | |
"other" => "" | |
]; | |
$events[] = $event; | |
} | |
return $events; | |
} | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment