Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering by categories using MetaQuery in GraphQl #28

Open
FuturisticWizard opened this issue Oct 5, 2024 · 1 comment
Open

Filtering by categories using MetaQuery in GraphQl #28

FuturisticWizard opened this issue Oct 5, 2024 · 1 comment

Comments

@FuturisticWizard
Copy link

FuturisticWizard commented Oct 5, 2024

Hello I have problem with filtering inside metaQuery . I asked a question on discord : https://discord.com/channels/838854618406453300/918553064703680542/1292095799169126461

Zrzut ekranu 2024-10-05 140523
Zrzut ekranu 2024-10-05 140625

I cant target categories/name. How to do that ? I beg for help !!!

@SamuelHadsall
Copy link

SamuelHadsall commented Oct 7, 2024

@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 );
		});```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants