Skip to content

Instantly share code, notes, and snippets.

@mvaneijgen

mvaneijgen/function.php

Last active Aug 18, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.