Skip to content

Instantly share code, notes, and snippets.

@mvaneijgen
Last active August 18, 2020 06:26
Show Gist options
  • Save mvaneijgen/a05f3205b9fe45936f8fb86978f08667 to your computer and use it in GitHub Desktop.
Save mvaneijgen/a05f3205b9fe45936f8fb86978f08667 to your computer and use it in GitHub Desktop.
Possible solution for multiple where: in GraphQL and Wordpress
<?php
// Register extra types
add_action('graphql_register_types', function () {
$customposttype_graphql_single_name = “MyCustomPostType”;
register_graphql_field('RootQueryTo' . $customposttype_graphql_single_name . 'ConnectionWhereArgs', 'postObjectIdTWO', [
'type' => 'ID',
'description' => __('The ID of the post object to filter by', 'your-textdomain'),
]);
})
add_filter('graphql_post_object_connection_query_args', function ($query_args, $source, $args, $context, $info) {
$post_object_id = $args['where']['postObjectId'];
$post_object_id_TWO = $args['where']['postObjectIdTWO'];
if (isset($post_object_id) && isset($post_object_id_TWO)) {
$query_args['meta_query'] = [
[
'key' => 'myCustomField',
'value' => $post_object_id,
'compare' => '='
],
[
'key' => 'myCustomFieldTWO',
'value' => $post_object_id_TWO,
'compare' => '='
],
];
}
return $query_args;
}, 10, 5);
query GetMyCustomPostType {
MyCustomPostType(where: {postObjectId: “123”, postObjectIdTWO: “456”}) {
nodes {
title
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment