wp-graphql / wp-graphql-meta-query

WPGraphQL Extension: Adds "meta_query" support to postObject connection queries using WP_Query
https://wpgraphql.com
GNU General Public License v3.0
53 stars 18 forks source link

Filtering by categories using MetaQuery in GraphQl #28

Open FuturisticWizard opened 1 month ago

FuturisticWizard commented 1 month ago

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