1

I have a custom taxonomy called collection, with advanced custom fields in it.

I'm using wordpress API, so since I need to filter the API responses by ACF values, I have this filter in my theme functions.php:

 add_filter( 'rest_collection_query', function( $args ) {
            $ignore = array('page', 'per_page', 'search', 'order', 'orderby', 'slug');
            foreach ( $_GET as $key => $value ) {
            if (!in_array($key, $ignore)) {
                $args['meta_query'][] = array(
                'key'   => $key,
                'value' => $value,
                );
            }
            }
            return $args; 
    });

This works great, except now in the WP admin, my taxonomy checkbox list no longer shows up for the custom post type to which it is registered.

I tried to wrap this block inside if(!is_admin()) {}, but that had no effect. Is there a specific syntax I should be using?

Note, I'm using gutenberg... could that be the reason?

  • @Pat_Morita You mean is_admin inside of add_filter? same result. Makes no difference. – geochanto Apr 10 at 19:43
  • You're aware that the filter you're using relates to a specific post type not a taxonomy? – Nicolai Apr 11 at 9:53
  • @Nicolai I used it for other post types. Then I used it on the taxonomy and it also worked... so not completely sure what you mean. Collection is a taxonomy. – geochanto Apr 11 at 20:32
  • 1
    See code reference rest_{$this->post_type}_query. "Targets a post collection request." From a certain type. Naming conflicts between taxonomies and post types have to be avoided. I saw that as an issue, or it seems like you have a post type and a taxonomy called collection, that's why I asked. By the way, not necessarily saying it has to do with the problem raised in your question. – Nicolai Apr 12 at 12:39
1

I'm using gutenberg... could that be the reason?

Yes, Gutenberg (or the Block Editor) uses the WordPress REST API when working with the taxonomy terms (and other things like custom post types).

So the filter rest_collection_query (or rest_{taxonomy}_query) will be applied no matter who/what made the REST API request, or where it was being made from (e.g. was it via /wp-admin/ or the site front-end?).

I'm using wordpress API

Assuming that you're making manual requests to the "collection" taxonomy endpoint (/wp-json/wp/v2/collection), you can distinguish the request's origin by using a custom parameter like so where the parameter name is my_key — and if the value is 1, then we can filter the $args (i.e. the query parameters):

add_filter( 'rest_collection_query', function( $args, $request ){
    if ( '1' === $request->get_param( 'my_key' ) ) {
        // your code here
    } // else, don't filter/modify $args

    return $args;
}, 10, 2 );

And then for example using the window.fetch() in JavaScript, you could do something like:

window.fetch( '/wp-json/wp/v2/collection?per_page=2&my_key=1' )
    .then( res => res.json() )
    .then( terms => console.log( terms ) );

Alternate Solution

Add your own custom endpoint and do whatever your heart desires..

The Classic Editor may help, but if a plugin or custom code performs a request to the exact same "collection" taxonomy endpoint, then you know what could happen. ;)

Last but not least..

By default, is_admin() returns false on a REST API endpoint/URL. So if for example you're on http://example.com/wp-json/wp/v2/posts (or that you make API request to that endpoint), then:

if ( ! is_admin() ) {
    // code here runs
}
  • sorry I have not checked on here in a couple of days. Yes, your solution does work. Though Meanwhile I ended up creating a custom endpoint anyway. Thanks for your help. Too bad the bounty expired :p – geochanto Apr 18 at 21:24
  • 1
    Yeah, that's tooo bad! 😆 Nope, just kidding. What really matters to me, is that my answer helped you. :) Cheers! – Sally CJ Apr 18 at 22:33
1

I'm going to answer my own question here. It appears is_admin does not work when using the gutenberg editor, because gutenberg also uses the API to make the queries.

I installed the classic editor, and the issue is resolved. Though it would be nice to know how to implement a similar check for gutenberg.

  • @SallyCJ I tried... but had a bit of a hard time following the logic what should go where, since your answer there is broken up into several parts of code. – geochanto Apr 14 at 22:19
  • Please ignore my previous comment and see my answer to this question - the custom parameter works for me and hopefully for you, too. Let me know. – Sally CJ Apr 15 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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