• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

actions-user
release: merge develop into master for v2.3.0

15905 of 18870 relevant lines covered (84.29%)

257.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.78
/src/Type/Connection/PostObjects.php
1
<?php
2

3
namespace WPGraphQL\Type\Connection;
4

5
use GraphQL\Type\Definition\ResolveInfo;
6
use WPGraphQL\AppContext;
7
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
8
use WPGraphQL\Data\DataSource;
9
use WPGraphQL\Model\Comment;
10
use WPGraphQL\Model\Post;
11
use WPGraphQL\Model\PostType;
12
use WPGraphQL\Model\User;
13
use WPGraphQL\Utils\Utils;
14
use WP_Post_Type;
15
use WP_Taxonomy;
16

17
/**
18
 * Class PostObjects
19
 *
20
 * This class organizes the registration of connections to PostObjects
21
 *
22
 * @package WPGraphQL\Type\Connection
23
 */
24
class PostObjects {
25

26
        /**
27
         * Registers the various connections from other Types to PostObjects
28
         *
29
         * @return void
30
         * @throws \Exception
31
         */
32
        public static function register_connections() {
596✔
33
                register_graphql_connection(
593✔
34
                        [
593✔
35
                                'fromType'       => 'ContentType',
593✔
36
                                'toType'         => 'ContentNode',
593✔
37
                                'fromFieldName'  => 'contentNodes',
593✔
38
                                'connectionArgs' => self::get_connection_args(),
593✔
39
                                'queryClass'     => 'WP_Query',
593✔
40
                                'resolve'        => static function ( PostType $post_type, $args, AppContext $context, ResolveInfo $info ) {
593✔
41
                                        $resolver = new PostObjectConnectionResolver( $post_type, $args, $context, $info );
×
42
                                        $resolver->set_query_arg( 'post_type', $post_type->name );
×
43

44
                                        return $resolver->get_connection();
×
45
                                },
593✔
46
                        ]
593✔
47
                );
593✔
48

49
                register_graphql_connection(
593✔
50
                        [
593✔
51
                                'fromType'      => 'Comment',
593✔
52
                                'toType'        => 'ContentNode',
593✔
53
                                'queryClass'    => 'WP_Query',
593✔
54
                                'oneToOne'      => true,
593✔
55
                                'fromFieldName' => 'commentedOn',
593✔
56
                                'resolve'       => static function ( Comment $comment, $args, AppContext $context, ResolveInfo $info ) {
593✔
57
                                        if ( empty( $comment->comment_post_ID ) || ! absint( $comment->comment_post_ID ) ) {
6✔
58
                                                return null;
×
59
                                        }
60
                                        $id       = absint( $comment->comment_post_ID );
6✔
61
                                        $resolver = new PostObjectConnectionResolver( $comment, $args, $context, $info, 'any' );
6✔
62

63
                                        return $resolver->one_to_one()->set_query_arg( 'p', $id )->set_query_arg( 'post_parent', null )->get_connection();
6✔
64
                                },
593✔
65
                        ]
593✔
66
                );
593✔
67

68
                register_graphql_connection(
593✔
69
                        [
593✔
70
                                'fromType'      => 'NodeWithRevisions',
593✔
71
                                'toType'        => 'ContentNode',
593✔
72
                                'fromFieldName' => 'revisionOf',
593✔
73
                                'description'   => static function () {
593✔
74
                                        return __( 'If the current node is a revision, this field exposes the node this is a revision of. Returns null if the node is not a revision of another node.', 'wp-graphql' );
19✔
75
                                },
593✔
76
                                'oneToOne'      => true,
593✔
77
                                'resolve'       => static function ( Post $post, $args, AppContext $context, ResolveInfo $info ) {
593✔
78
                                        if ( ! $post->isRevision || ! isset( $post->parentDatabaseId ) || ! absint( $post->parentDatabaseId ) ) {
2✔
79
                                                return null;
×
80
                                        }
81

82
                                        $resolver = new PostObjectConnectionResolver( $post, $args, $context, $info );
2✔
83
                                        $resolver->set_query_arg( 'p', $post->parentDatabaseId );
2✔
84

85
                                        return $resolver->one_to_one()->get_connection();
2✔
86
                                },
593✔
87
                        ]
593✔
88
                );
593✔
89

90
                register_graphql_connection(
593✔
91
                        [
593✔
92
                                'fromType'       => 'RootQuery',
593✔
93
                                'toType'         => 'ContentNode',
593✔
94
                                'queryClass'     => 'WP_Query',
593✔
95
                                'fromFieldName'  => 'contentNodes',
593✔
96
                                'connectionArgs' => self::get_connection_args(),
593✔
97
                                'resolve'        => static function ( $source, $args, $context, $info ) {
593✔
98
                                        $post_types = isset( $args['where']['contentTypes'] ) && is_array( $args['where']['contentTypes'] ) ? $args['where']['contentTypes'] : \WPGraphQL::get_allowed_post_types();
5✔
99

100
                                        return DataSource::resolve_post_objects_connection( $source, $args, $context, $info, $post_types );
5✔
101
                                },
593✔
102
                        ]
593✔
103
                );
593✔
104

105
                register_graphql_connection(
593✔
106
                        [
593✔
107
                                'fromType'           => 'HierarchicalContentNode',
593✔
108
                                'toType'             => 'ContentNode',
593✔
109
                                'fromFieldName'      => 'parent',
593✔
110
                                'connectionTypeName' => 'HierarchicalContentNodeToParentContentNodeConnection',
593✔
111
                                'description'        => static function () {
593✔
112
                                        return __( 'The parent of the node. The parent object can be of various types', 'wp-graphql' );
19✔
113
                                },
593✔
114
                                'oneToOne'           => true,
593✔
115
                                'resolve'            => static function ( Post $post, $args, AppContext $context, ResolveInfo $info ) {
593✔
116
                                        if ( ! isset( $post->parentDatabaseId ) || ! absint( $post->parentDatabaseId ) ) {
7✔
117
                                                return null;
3✔
118
                                        }
119

120
                                        $resolver = new PostObjectConnectionResolver( $post, $args, $context, $info );
5✔
121
                                        $resolver->set_query_arg( 'p', $post->parentDatabaseId );
5✔
122

123
                                        return $resolver->one_to_one()->get_connection();
5✔
124
                                },
593✔
125
                        ]
593✔
126
                );
593✔
127

128
                register_graphql_connection(
593✔
129
                        [
593✔
130
                                'fromType'           => 'HierarchicalContentNode',
593✔
131
                                'fromFieldName'      => 'children',
593✔
132
                                'toType'             => 'ContentNode',
593✔
133
                                'connectionTypeName' => 'HierarchicalContentNodeToContentNodeChildrenConnection',
593✔
134
                                'connectionArgs'     => self::get_connection_args(),
593✔
135
                                'queryClass'         => 'WP_Query',
593✔
136
                                'resolve'            => static function ( Post $post, $args, $context, $info ) {
593✔
137
                                        if ( $post->isRevision ) {
1✔
138
                                                $id = $post->parentDatabaseId;
×
139
                                        } else {
140
                                                $id = $post->databaseId;
1✔
141
                                        }
142

143
                                        $resolver = new PostObjectConnectionResolver( $post, $args, $context, $info, 'any' );
1✔
144
                                        $resolver->set_query_arg( 'post_parent', $id );
1✔
145

146
                                        return $resolver->get_connection();
1✔
147
                                },
593✔
148
                        ]
593✔
149
                );
593✔
150

151
                register_graphql_connection(
593✔
152
                        [
593✔
153
                                'fromType'           => 'HierarchicalContentNode',
593✔
154
                                'toType'             => 'ContentNode',
593✔
155
                                'fromFieldName'      => 'ancestors',
593✔
156
                                'connectionArgs'     => self::get_connection_args(),
593✔
157
                                'connectionTypeName' => 'HierarchicalContentNodeToContentNodeAncestorsConnection',
593✔
158
                                'queryClass'         => 'WP_Query',
593✔
159
                                'description'        => static function () {
593✔
160
                                        return __( 'Returns ancestors of the node. Default ordered as lowest (closest to the child) to highest (closest to the root).', 'wp-graphql' );
19✔
161
                                },
593✔
162
                                'resolve'            => static function ( Post $post, $args, $context, $info ) {
593✔
163
                                        $ancestors = isset( $post->databaseId ) ? get_ancestors( $post->databaseId, '', 'post_type' ) : null;
1✔
164
                                        if ( empty( $ancestors ) || ! is_array( $ancestors ) ) {
1✔
165
                                                return null;
×
166
                                        }
167
                                        $resolver = new PostObjectConnectionResolver( $post, $args, $context, $info );
1✔
168
                                        $resolver->set_query_arg( 'post__in', $ancestors );
1✔
169
                                        $resolver->set_query_arg( 'orderby', 'post__in' );
1✔
170

171
                                        return $resolver->get_connection();
1✔
172
                                },
593✔
173
                        ]
593✔
174
                );
593✔
175

176
                /**
177
                 * Register Connections to PostObjects.
178
                 */
179
                $allowed_post_types = \WPGraphQL::get_allowed_post_types( 'objects' );
593✔
180

181
                foreach ( $allowed_post_types as $post_type_object ) {
593✔
182

183
                        /**
184
                         * Registers the RootQuery connection for each post_type
185
                         */
186
                        if ( true === $post_type_object->graphql_register_root_connection && 'revision' !== $post_type_object->name ) {
593✔
187
                                $root_query_from_field_name = Utils::format_field_name( $post_type_object->graphql_plural_name );
593✔
188

189
                                // Prevent field name conflicts with the singular PostObject type.
190
                                if ( $post_type_object->graphql_single_name === $post_type_object->graphql_plural_name ) {
593✔
191
                                        $root_query_from_field_name = 'all' . ucfirst( $post_type_object->graphql_single_name );
4✔
192
                                }
193

194
                                register_graphql_connection(
593✔
195
                                        self::get_connection_config(
593✔
196
                                                $post_type_object,
593✔
197
                                                [
593✔
198
                                                        'fromFieldName' => $root_query_from_field_name,
593✔
199
                                                ]
593✔
200
                                        )
593✔
201
                                );
593✔
202
                        }
203

204
                        /**
205
                         * Any post type that supports author should have a connection from User->Author
206
                         */
207
                        if ( true === post_type_supports( $post_type_object->name, 'author' ) ) {
593✔
208

209
                                /**
210
                                 * Registers the User connection for each post_type
211
                                 */
212
                                register_graphql_connection(
596✔
213
                                        self::get_connection_config(
596✔
214
                                                $post_type_object,
596✔
215
                                                [
596✔
216
                                                        'fromType' => 'User',
596✔
217
                                                        'resolve'  => static function ( User $user, $args, AppContext $context, ResolveInfo $info ) use ( $post_type_object ) {
596✔
218
                                                                $resolver = new PostObjectConnectionResolver( $user, $args, $context, $info, $post_type_object->name );
4✔
219
                                                                $resolver->set_query_arg( 'author', $user->databaseId );
4✔
220

221
                                                                return $resolver->get_connection();
4✔
222
                                                        },
596✔
223
                                                ]
596✔
224
                                        )
596✔
225
                                );
596✔
226
                        }
227
                }
228
        }
229

230
        /**
231
         * Given the Post Type Object and an array of args, this returns an array of args for use in
232
         * registering a connection.
233
         *
234
         * @param mixed|\WP_Post_Type|\WP_Taxonomy $graphql_object The post type object for the post_type having a
235
         * connection registered to it
236
         * @param array<string,mixed>              $args           The custom args to modify the connection registration
237
         *
238
         * @return array<string,mixed>
239
         */
240
        public static function get_connection_config( $graphql_object, $args = [] ) {
607✔
241
                $connection_args = self::get_connection_args( [], $graphql_object );
593✔
242

243
                if ( 'revision' === $graphql_object->name ) {
593✔
244
                        unset( $connection_args['status'] );
×
245
                        unset( $connection_args['stati'] );
×
246
                }
247

248
                return array_merge(
607✔
249
                        [
607✔
250
                                'fromType'       => 'RootQuery',
607✔
251
                                'toType'         => $graphql_object->graphql_single_name,
607✔
252
                                'queryClass'     => 'WP_Query',
607✔
253
                                'fromFieldName'  => lcfirst( $graphql_object->graphql_plural_name ),
607✔
254
                                'connectionArgs' => $connection_args,
607✔
255
                                'resolve'        => static function ( $root, $args, $context, $info ) use ( $graphql_object ) {
607✔
256
                                        return DataSource::resolve_post_objects_connection( $root, $args, $context, $info, $graphql_object->name );
109✔
257
                                },
607✔
258
                        ],
607✔
259
                        $args
607✔
260
                );
607✔
261
        }
262

263
        /**
264
         * Given an optional array of args, this returns the args to be used in the connection
265
         *
266
         * @param array<string,array<string,mixed>> $args             The args to modify the defaults
267
         * @param mixed|\WP_Post_Type|\WP_Taxonomy  $post_type_object The post type the connection is going to
268
         *
269
         * @return array<string,array<string,mixed>>
270
         */
271
        public static function get_connection_args( $args = [], $post_type_object = null ) {
593✔
272
                $fields = [
593✔
273
                        /**
274
                         * Search Parameter
275
                         *
276
                         * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
277
                         * @since 0.0.5
278
                         */
279
                        'search'      => [
593✔
280
                                'name'        => 'search',
593✔
281
                                'type'        => 'String',
593✔
282
                                'description' => static function () {
593✔
283
                                        return __( 'Show Posts based on a keyword search', 'wp-graphql' );
23✔
284
                                },
593✔
285
                        ],
593✔
286

287
                        /**
288
                         * Post & Page Parameters
289
                         *
290
                         * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
291
                         * @since 0.0.5
292
                         */
293
                        'id'          => [
593✔
294
                                'type'        => 'Int',
593✔
295
                                'description' => static function () {
593✔
296
                                        return __( 'Specific database ID of the object', 'wp-graphql' );
23✔
297
                                },
593✔
298
                        ],
593✔
299
                        'in'          => [
593✔
300
                                'type'        => [
593✔
301
                                        'list_of' => 'ID',
593✔
302
                                ],
593✔
303
                                'description' => static function () {
593✔
304
                                        return __( 'Array of IDs for the objects to retrieve', 'wp-graphql' );
23✔
305
                                },
593✔
306
                        ],
593✔
307
                        'notIn'       => [
593✔
308
                                'type'        => [
593✔
309
                                        'list_of' => 'ID',
593✔
310
                                ],
593✔
311
                                'description' => static function () {
593✔
312
                                        return __( 'Specify IDs NOT to retrieve. If this is used in the same query as "in", it will be ignored', 'wp-graphql' );
23✔
313
                                },
593✔
314
                        ],
593✔
315
                        'name'        => [
593✔
316
                                'type'        => 'String',
593✔
317
                                'description' => static function () {
593✔
318
                                        return __( 'Slug / post_name of the object', 'wp-graphql' );
23✔
319
                                },
593✔
320
                        ],
593✔
321
                        'nameIn'      => [
593✔
322
                                'type'        => [
593✔
323
                                        'list_of' => 'String',
593✔
324
                                ],
593✔
325
                                'description' => static function () {
593✔
326
                                        return __( 'Specify objects to retrieve. Use slugs', 'wp-graphql' );
23✔
327
                                },
593✔
328
                        ],
593✔
329
                        'parent'      => [
593✔
330
                                'type'        => 'ID',
593✔
331
                                'description' => static function () {
593✔
332
                                        return __( 'Use ID to return only children. Use 0 to return only top-level items', 'wp-graphql' );
23✔
333
                                },
593✔
334
                        ],
593✔
335
                        'parentIn'    => [
593✔
336
                                'type'        => [
593✔
337
                                        'list_of' => 'ID',
593✔
338
                                ],
593✔
339
                                'description' => static function () {
593✔
340
                                        return __( 'Specify objects whose parent is in an array', 'wp-graphql' );
23✔
341
                                },
593✔
342
                        ],
593✔
343
                        'parentNotIn' => [
593✔
344
                                'type'        => [
593✔
345
                                        'list_of' => 'ID',
593✔
346
                                ],
593✔
347
                                'description' => static function () {
593✔
348
                                        return __( 'Specify posts whose parent is not in an array', 'wp-graphql' );
23✔
349
                                },
593✔
350
                        ],
593✔
351
                        'title'       => [
593✔
352
                                'type'        => 'String',
593✔
353
                                'description' => static function () {
593✔
354
                                        return __( 'Title of the object', 'wp-graphql' );
23✔
355
                                },
593✔
356
                        ],
593✔
357

358
                        /**
359
                         * Password parameters
360
                         *
361
                         * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Password_Parameters
362
                         * @since 0.0.2
363
                         */
364
                        'hasPassword' => [
593✔
365
                                'type'        => 'Boolean',
593✔
366
                                'description' => static function () {
593✔
367
                                        return __( 'True for objects with passwords; False for objects without passwords; null for all objects with or without passwords', 'wp-graphql' );
23✔
368
                                },
593✔
369
                        ],
593✔
370
                        'password'    => [
593✔
371
                                'type'        => 'String',
593✔
372
                                'description' => static function () {
593✔
373
                                        return __( 'Show posts with a specific password.', 'wp-graphql' );
23✔
374
                                },
593✔
375
                        ],
593✔
376

377
                        /**
378
                         * NOTE: post_type is intentionally not supported on connections to Single post types as
379
                         * the connection to the singular Post Type already sets this argument as the entry
380
                         * point to the Graph
381
                         *
382
                         * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters
383
                         * @since 0.0.2
384
                         */
385

386
                        /**
387
                         * Status parameters
388
                         *
389
                         * @see   : https://developer.wordpress.org/reference/classes/wp_query/#status-parameters
390
                         * @since 0.0.2
391
                         */
392
                        'status'      => [
593✔
393
                                'type'        => 'PostStatusEnum',
593✔
394
                                'description' => static function () {
593✔
395
                                        return __( 'Show posts with a specific status.', 'wp-graphql' );
23✔
396
                                },
593✔
397
                        ],
593✔
398
                        'stati'       => [
593✔
399
                                'type'        => [
593✔
400
                                        'list_of' => 'PostStatusEnum',
593✔
401
                                ],
593✔
402
                                'description' => static function () {
593✔
403
                                        return __( 'Retrieve posts where post status is in an array.', 'wp-graphql' );
23✔
404
                                },
593✔
405
                        ],
593✔
406

407
                        /**
408
                         * Order & Orderby parameters
409
                         *
410
                         * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
411
                         * @since 0.0.2
412
                         */
413
                        'orderby'     => [
593✔
414
                                'type'        => [
593✔
415
                                        'list_of' => 'PostObjectsConnectionOrderbyInput',
593✔
416
                                ],
593✔
417
                                'description' => static function () {
593✔
418
                                        return __( 'What parameter to use to order the objects by.', 'wp-graphql' );
23✔
419
                                },
593✔
420
                        ],
593✔
421

422
                        /**
423
                         * Date parameters
424
                         *
425
                         * @see https://developer.wordpress.org/reference/classes/wp_query/#date-parameters
426
                         */
427
                        'dateQuery'   => [
593✔
428
                                'type'        => 'DateQueryInput',
593✔
429
                                'description' => static function () {
593✔
430
                                        return __( 'Filter the connection based on dates', 'wp-graphql' );
22✔
431
                                },
593✔
432
                        ],
593✔
433

434
                        /**
435
                         * Mime type parameters
436
                         *
437
                         * @see https://developer.wordpress.org/reference/classes/wp_query/#mime-type-parameters
438
                         */
439
                        'mimeType'    => [
593✔
440
                                'type'        => 'MimeTypeEnum',
593✔
441
                                'description' => static function () {
593✔
442
                                        return __( 'Get objects with a specific mimeType property', 'wp-graphql' );
23✔
443
                                },
593✔
444
                        ],
593✔
445
                ];
593✔
446

447
                /**
448
                 * If the connection is to a single post type, add additional arguments.
449
                 *
450
                 * If the connection is to many post types, the `$post_type_object` will not be an instance
451
                 * of \WP_Post_Type, and we should not add these additional arguments because it
452
                 * confuses the connection args for connections of plural post types.
453
                 *
454
                 * For example, if you have one Post Type that supports author and another that doesn't
455
                 * we don't want to expose the `author` filter for a plural connection of multiple post types
456
                 * as it's misleading to be able to filter by author on a post type that doesn't have
457
                 * authors.
458
                 *
459
                 * If folks want to enable these arguments, they can filter them back in per-connection, but
460
                 * by default WPGraphQL is exposing the least common denominator (the fields that are shared
461
                 * by _all_ post types in a multi-post-type connection)
462
                 *
463
                 * Here's a practical example:
464
                 *
465
                 * Lets's say you register a "House" post type and it doesn't support author.
466
                 *
467
                 * The "House" Post Type will show in the `contentNodes` connection, which is a connection
468
                 * to many post types.
469
                 *
470
                 * We could (pseudo code) query like so:
471
                 *
472
                 * {
473
                 *   contentNodes( where: { contentTypes: [ HOUSE ] ) {
474
                 *     nodes {
475
                 *       id
476
                 *       title
477
                 *       ...on House {
478
                 *         ...someHouseFields
479
                 *       }
480
                 *     }
481
                 *   }
482
                 * }
483
                 *
484
                 * But since houses don't have authors, it doesn't make sense to have WPGraphQL expose the
485
                 * ability to query four houses filtered by author.
486
                 *
487
                 * ```
488
                 *{
489
                 *   contentNodes( where: { author: "some author input" contentTypes: [ HOUSE ] ) {
490
                 *     nodes {
491
                 *       id
492
                 *       title
493
                 *       ...on House {
494
                 *         ...someHouseFields
495
                 *       }
496
                 *     }
497
                 *   }
498
                 * }
499
                 * ```
500
                 *
501
                 * We want to output filters on connections based on what's actually possible, and filtering
502
                 * houses by author isn't possible, so exposing it in the Schema is quite misleading to
503
                 * consumers.
504
                 */
505
                if ( isset( $post_type_object ) && $post_type_object instanceof WP_Post_Type ) {
593✔
506

507
                        /**
508
                         * Add arguments to post types that support author
509
                         */
510
                        if ( true === post_type_supports( $post_type_object->name, 'author' ) ) {
593✔
511
                                /**
512
                                 * Author $args
513
                                 *
514
                                 * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
515
                                 * @since 0.0.5
516
                                 */
517
                                $fields['author']      = [
593✔
518
                                        'type'        => 'Int',
593✔
519
                                        'description' => static function () {
593✔
520
                                                return __( 'The user that\'s connected as the author of the object. Use the userId for the author object.', 'wp-graphql' );
17✔
521
                                        },
593✔
522
                                ];
593✔
523
                                $fields['authorName']  = [
593✔
524
                                        'type'        => 'String',
593✔
525
                                        'description' => static function () {
593✔
526
                                                return __( 'Find objects connected to the author by the author\'s nicename', 'wp-graphql' );
17✔
527
                                        },
593✔
528
                                ];
593✔
529
                                $fields['authorIn']    = [
593✔
530
                                        'type'        => [
593✔
531
                                                'list_of' => 'ID',
593✔
532
                                        ],
593✔
533
                                        'description' => static function () {
593✔
534
                                                return __( 'Find objects connected to author(s) in the array of author\'s userIds', 'wp-graphql' );
17✔
535
                                        },
593✔
536
                                ];
593✔
537
                                $fields['authorNotIn'] = [
593✔
538
                                        'type'        => [
593✔
539
                                                'list_of' => 'ID',
593✔
540
                                        ],
593✔
541
                                        'description' => static function () {
593✔
542
                                                return __( 'Find objects NOT connected to author(s) in the array of author\'s userIds', 'wp-graphql' );
17✔
543
                                        },
593✔
544
                                ];
593✔
545
                        }
546

547
                        $connected_taxonomies = get_object_taxonomies( $post_type_object->name );
593✔
548
                        if ( ! empty( $connected_taxonomies ) && in_array( 'category', $connected_taxonomies, true ) ) {
593✔
549
                                /**
550
                                 * Category $args
551
                                 *
552
                                 * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
553
                                 * @since 0.0.5
554
                                 */
555
                                $fields['categoryId']    = [
593✔
556
                                        'type'        => 'Int',
593✔
557
                                        'description' => static function () {
593✔
558
                                                return __( 'Category ID', 'wp-graphql' );
16✔
559
                                        },
593✔
560
                                ];
593✔
561
                                $fields['categoryName']  = [
593✔
562
                                        'type'        => 'String',
593✔
563
                                        'description' => static function () {
593✔
564
                                                return __( 'Use Category Slug', 'wp-graphql' );
16✔
565
                                        },
593✔
566
                                ];
593✔
567
                                $fields['categoryIn']    = [
593✔
568
                                        'type'        => [
593✔
569
                                                'list_of' => 'ID',
593✔
570
                                        ],
593✔
571
                                        'description' => static function () {
593✔
572
                                                return __( 'Array of category IDs, used to display objects from one category OR another', 'wp-graphql' );
16✔
573
                                        },
593✔
574
                                ];
593✔
575
                                $fields['categoryNotIn'] = [
593✔
576
                                        'type'        => [
593✔
577
                                                'list_of' => 'ID',
593✔
578
                                        ],
593✔
579
                                        'description' => static function () {
593✔
580
                                                return __( 'Array of category IDs, used to display objects from one category OR another', 'wp-graphql' );
16✔
581
                                        },
593✔
582
                                ];
593✔
583
                        }
584

585
                        if ( ! empty( $connected_taxonomies ) && in_array( 'post_tag', $connected_taxonomies, true ) ) {
593✔
586
                                /**
587
                                 * Tag $args
588
                                 *
589
                                 * @see   : https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
590
                                 * @since 0.0.5
591
                                 */
592
                                $fields['tag']        = [
593✔
593
                                        'type'        => 'String',
593✔
594
                                        'description' => static function () {
593✔
595
                                                return __( 'Tag Slug', 'wp-graphql' );
16✔
596
                                        },
593✔
597
                                ];
593✔
598
                                $fields['tagId']      = [
593✔
599
                                        'type'        => 'String',
593✔
600
                                        'description' => static function () {
593✔
601
                                                return __( 'Use Tag ID', 'wp-graphql' );
16✔
602
                                        },
593✔
603
                                ];
593✔
604
                                $fields['tagIn']      = [
593✔
605
                                        'type'        => [
593✔
606
                                                'list_of' => 'ID',
593✔
607
                                        ],
593✔
608
                                        'description' => static function () {
593✔
609
                                                return __( 'Array of tag IDs, used to display objects from one tag OR another', 'wp-graphql' );
16✔
610
                                        },
593✔
611
                                ];
593✔
612
                                $fields['tagNotIn']   = [
593✔
613
                                        'type'        => [
593✔
614
                                                'list_of' => 'ID',
593✔
615
                                        ],
593✔
616
                                        'description' => static function () {
593✔
617
                                                return __( 'Array of tag IDs, used to display objects from one tag OR another', 'wp-graphql' );
16✔
618
                                        },
593✔
619
                                ];
593✔
620
                                $fields['tagSlugAnd'] = [
593✔
621
                                        'type'        => [
593✔
622
                                                'list_of' => 'String',
593✔
623
                                        ],
593✔
624
                                        'description' => static function () {
593✔
625
                                                return __( 'Array of tag slugs, used to display objects from one tag AND another', 'wp-graphql' );
16✔
626
                                        },
593✔
627
                                ];
593✔
628
                                $fields['tagSlugIn']  = [
593✔
629
                                        'type'        => [
593✔
630
                                                'list_of' => 'String',
593✔
631
                                        ],
593✔
632
                                        'description' => static function () {
593✔
633
                                                return __( 'Array of tag slugs, used to include objects in ANY specified tags', 'wp-graphql' );
16✔
634
                                        },
593✔
635
                                ];
593✔
636
                        }
637
                } elseif ( $post_type_object instanceof WP_Taxonomy ) {
593✔
638
                        /**
639
                         * Taxonomy-specific Content Type $args
640
                         *
641
                         * @see   : https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
642
                         */
643
                        $args['contentTypes'] = [
593✔
644
                                'type'        => [ 'list_of' => 'ContentTypesOf' . \WPGraphQL\Utils\Utils::format_type_name( $post_type_object->graphql_single_name ) . 'Enum' ],
593✔
645
                                'description' => static function () {
593✔
646
                                        return __( 'The Types of content to filter', 'wp-graphql' );
15✔
647
                                },
593✔
648
                        ];
593✔
649
                } else {
650
                        /**
651
                         * Handle cases when the connection is for many post types
652
                         */
653

654
                        /**
655
                         * Content Type $args
656
                         *
657
                         * @see   : https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
658
                         */
659
                        $args['contentTypes'] = [
593✔
660
                                'type'        => [ 'list_of' => 'ContentTypeEnum' ],
593✔
661
                                'description' => static function () {
593✔
662
                                        return __( 'The Types of content to filter', 'wp-graphql' );
18✔
663
                                },
593✔
664
                        ];
593✔
665
                }
666

667
                return array_merge( $fields, $args );
593✔
668
        }
669
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc