Skip to content

Instantly share code, notes, and snippets.

@hsimah
Last active June 7, 2021 01:32
Show Gist options
  • Save hsimah/7bed1a13c8b87439086820a54c923e93 to your computer and use it in GitHub Desktop.
Save hsimah/7bed1a13c8b87439086820a54c923e93 to your computer and use it in GitHub Desktop.
Register custom query inputs
// this assumes you have registered a Type called YourPostType
add_filter('graphql_input_fields', function ($fields, $type_name, $config) {
if (isset($config['queryClass']) && 'WP_Query' === $config['queryClass']) {
if ('RootQueryToYourPostTypeConnectionWhereArgs' === $type_name) {
$fields['custom_attribute'] = [
'name' => 'custom_attribute',
'type' => 'Boolean',
'description' => __('A custom boolean attribute', 'hello-there'),
];
}
}
return $fields;
}, 10, 3);
// Map inputs to WP Query
add_filter('graphql_map_input_fields_to_wp_query', function ($query_args) {
if (array_key_exists('custom_attribute', $query_args)) {
$custom_attribute = $query_args['custom_attribute'];
if (!empty($custom_attribute)) {
$query_args['custom_attribute'] = $custom_attribute;
}
}
return $query_args;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment