-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-graphql-homepage.php
51 lines (42 loc) · 1.43 KB
/
wp-graphql-homepage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Plugin Name: WPGraphql Homepage
* Plugin URI: http://github.com/ashhitch/wp-graphql-homepage
* Description: A WPGraphQL Extension that adds a root query to the GraphQL schema that returns the front page
* Author: Ash Hitchcock
* Author URI: https://www.ashleyhitchcock.com
* Text Domain: wp-graphql-homepage
* Domain Path: /languages
* Version: 1.0.0
*
* @package WP_Graphql_Homepage
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
add_action( 'graphql_register_types', function() {
register_graphql_field( 'RootQuery', 'homepage', [
'type' => 'Page',
'description' => __( 'Returns the homepage', 'wp-graphql-homepage' ),
'resolve' => function($item, array $args, AppContext $context) {
$page_on_front = get_option( 'page_on_front', 0 );
if ($page_on_front == 0 ) {
return null;
}
return DataSource::resolve_post_object( $page_on_front, $context );
},
]);
register_graphql_field( 'RootQuery', 'pageForPosts', [
'type' => 'Page',
'description' => __( 'Returns the page for posts', 'wp-graphql-homepage' ),
'resolve' => function($item, array $args, AppContext $context) {
$page_for_posts = get_option( 'page_for_posts', 0 );
if ($page_for_posts == 0 ) {
return null;
}
return DataSource::resolve_post_object( $page_for_posts, $context );
},
]);
} );