0

I'm trying to do the above-mentioned; and came across this and this. Is it still the case that you have to query by term ID using the REST API, and there's no built-in way to query posts via the WP REST API using taxonomy slugs?

1 Answer 1

1

For the moment, I've created this workaround here thanks to this post here. It's basically the same, just a little more general.

Assuming a custom post type with the your_custom_post_type slug, and three custom taxonomies with these names here:

  • first_custom_taxonomy_name
  • second_custom_taxonomy_name
  • third_custom_taxonomy_name

The code below enables to query via their slug value if you query not via their $taxonomy_name in the REST API, but via {$taxonomy_name}_slug.

More precisely:

  • Using the taxonomy name in the REST endpoint, you'd have to use the ID:

http://example.org/wp-json/wp/v2/your_custom_post_type?first_custom_taxonomy_name=12

  • Using the taxonomy name + _slug suffix, you can query in f(slug):

http://example.org/wp-json/wp/v2/your_custom_post_type?first_custom_taxonomy_name_slug=slug-value

/**
             * Querying via taxonomy slug is by default not possible in WP, we have to create a
             * filter to do so
             *
             * @param  array            $args     array of arguments to be used in WP_Query
             * @param  WP_REST_Request  $request  The REST API Request
             *
             * @return array $args array of arguments to be used in WP_Query, updated by this function
             */
            
            add_filter(
                "rest_your_custom_post_type_query",
                function (
                    array $args,
                    WP_REST_Request $request
                ): array {
                    
                    // Get parameters of the query
                    $params = $request->get_params();
                    
                    // Enable this feature for all of the custom taxonomies
                    $custom_taxonomies = [
                        'first_custom_taxonomy_name',
                        'second_custom_taxonomy_name',
                        'third_custom_taxonomy_name'
                    ];
                    
                    foreach ( $custom_taxonomies as $custom_taxonomy ) {
                        
                        if ( isset( $params["{$custom_taxonomy}_slug"] ) ) {
                            
                            $args['tax_query'][] = [
                                'taxonomy' => $custom_taxonomy,
                                'field'    => 'slug',
                                'terms'    => explode(
                                    ",",
                                    $params["{$custom_taxonomy}_slug"]
                                )
                            ];
                            
                        }
                        
                    }
                    
                    return $args;
                    
                },
                10,
                2
            );

You can easily extend this with additional taxonomies, just by adding the respective taxonomy name into $custom_taxonomies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.