Querying posts by custom field with WPGraphQL #621
-
Greetings! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Thanks for your question, Nazar. Atlas Content Modeler does not support 'where' queries for field values via WPGraphQL by default. If you need this feature now, you would have to write some custom PHP code following a guide like this one for ACF. The example adds support for the 'where' argument for a named ACF field that appears on the core Post type. It could be adapted for ACM's fields and custom post types. Note that altering WPGraphQL or ACM behavior with custom code carries some risks (your code could break after updates to WPGraphQL or ACM), but here's a quick example if you want to explore it further:
This PHP code in your active theme's functions.php (or in a custom plugin) enables that: add_action( 'graphql_register_types', 'custom_acm_register_where_query' );
function custom_acm_register_where_query() {
$acm_model_graphql_singular_name = 'Rabbit';
$acm_field_name = 'color';
register_graphql_field(
'RootQueryTo' . $acm_model_graphql_singular_name . 'ConnectionWhereArgs',
$acm_field_name,
[
'type' => 'String',
'description' => __( 'The color of the rabbit to filter by', 'your-textdomain' ),
]
);
}
add_filter( 'graphql_post_object_connection_query_args', 'custom_acm_filter_connection_query', 10, 3 );
function custom_acm_filter_connection_query( $query_args, $source, $args ) {
$acm_field_name = 'color';
$where_query_value = $args['where'][$acm_field_name];
if ( isset( $where_query_value) ) {
$query_args['meta_query'] = [
[
'key' => $acm_field_name,
'value' => $where_query_value,
'compare' => '='
]
];
}
return $query_args;
} Allowing you to do queries like:
And get the IDs of the two black rabbits in response:
|
Beta Was this translation helpful? Give feedback.
-
Just to add that an alternative to querying by custom field values is to set up taxonomies for your models and write queries for those. This requires an extra plugin called WPGraphQL Tax Query but no custom code. ACM already supports custom taxonomies: https://developers.wpengine.com/docs/atlas-content-modeler/tutorial/models/taxonomies/create WPGraphQL Taxonomy queries with ACM are documented here: https://developers.wpengine.com/docs/atlas-content-modeler/tutorial/models/taxonomies/query Taxonomy queries are often faster than field value queries too. |
Beta Was this translation helpful? Give feedback.
Thanks for your question, Nazar.
Atlas Content Modeler does not support 'where' queries for field values via WPGraphQL by default.
If you need this feature now, you would have to write some custom PHP code following a guide like this one for ACF. The example adds support for the 'where' argument for a named ACF field that appears on the core Post type. It could be adapted for ACM's fields and custom post types.
Note that altering WPGraphQL or ACM behavior with custom code carries some risks (your code could break after updates to WPGraphQL or ACM), but here's a quick example if you want to explore it further: