Open FuturisticWizard opened 1 month ago
@FuturisticWizard so categories is the meta_key that is stored in the post_meta table correct? Is this an acf field? If that's the case, to be honest I would just use a hook and create your own custom where clause on your blogposts node
register_graphql_field('RootQueryToRatingReviewConnectionWhereArgs');
Below I had to add a stateMoveFrom and a cityMovedFrom which are acf fields on custom post type. Then you can build out a meta_query
// Register the custom where argument for the stateMovedFrom field
register_graphql_field( 'RootQueryToRatingReviewConnectionWhereArgs', 'stateMovedFrom', [
'type' => 'String',
'description' => __( 'Filter rating reviews by stateMovedFrom ACF field', $this->plugin_name ),
]);
// Register the custom where argument for the cityMovedFrom field
register_graphql_field( 'RootQueryToRatingReviewConnectionWhereArgs', 'cityMovedFrom', [
'type' => 'String',
'description' => __( 'Filter rating reviews by cityMovedFrom ACF field', $this->plugin_name ),
]);
// Modify the query args for the ratingReviews connection
add_filter( 'graphql_post_object_connection_query_args', function( $query_args, $source, $input, $context, $info ) {
// Check if this filter is applied to the desired post type connection
if ( isset( $info->fieldName ) && 'ratingReviews' === $info->fieldName ) {
// Check if the stateMovedFrom custom where argument is set
if ( ! empty( $input['where']['stateMovedFrom'] ) ) {
$meta_query = [
'key' => 'state_moved_from', // The ACF field name for state
'value' => $input['where']['stateMovedFrom'], // The value to match
'compare' => '=' // Comparison operator
];
// If a meta query already exists, add to it. Otherwise, create one.
if ( isset( $query_args['meta_query'] ) ) {
$query_args['meta_query'][] = $meta_query;
} else {
$query_args['meta_query'] = [ $meta_query ];
}
}
// Check if the cityMovedFrom custom where argument is set
if ( ! empty( $input['where']['cityMovedFrom'] ) ) {
$meta_query = [
'key' => 'city_moved_from', // The ACF field name for city
'value' => $input['where']['cityMovedFrom'], // The value to match
'compare' => '=' // Comparison operator
];
// If a meta query already exists, add to it. Otherwise, create one.
if ( isset( $query_args['meta_query'] ) ) {
$query_args['meta_query'][] = $meta_query;
} else {
$query_args['meta_query'] = [ $meta_query ];
}
}
}
return $query_args;
}, 10, 5 );
});```
Hello I have problem with filtering inside metaQuery . I asked a question on discord : https://discord.com/channels/838854618406453300/918553064703680542/1292095799169126461
I cant target categories/name. How to do that ? I beg for help !!!