admin管理员组

文章数量:1023221

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?

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?

Share Improve this question edited Apr 10, 2019 at 19:51 geochanto asked Apr 6, 2019 at 22:36 geochantogeochanto 13115 bronze badges 4
  • @Pat_Morita You mean is_admin inside of add_filter? same result. Makes no difference. – geochanto Commented Apr 10, 2019 at 19:43
  • You're aware that the filter you're using relates to a specific post type not a taxonomy? – Nicolai Grossherr Commented Apr 11, 2019 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 Commented Apr 11, 2019 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 Grossherr Commented Apr 12, 2019 at 12:39
Add a comment  | 

2 Answers 2

Reset to default 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/wp-json/wp/v2/posts (or that you make API request to that endpoint), then:

if ( ! is_admin() ) {
    // code here runs
}

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.

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?

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?

Share Improve this question edited Apr 10, 2019 at 19:51 geochanto asked Apr 6, 2019 at 22:36 geochantogeochanto 13115 bronze badges 4
  • @Pat_Morita You mean is_admin inside of add_filter? same result. Makes no difference. – geochanto Commented Apr 10, 2019 at 19:43
  • You're aware that the filter you're using relates to a specific post type not a taxonomy? – Nicolai Grossherr Commented Apr 11, 2019 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 Commented Apr 11, 2019 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 Grossherr Commented Apr 12, 2019 at 12:39
Add a comment  | 

2 Answers 2

Reset to default 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/wp-json/wp/v2/posts (or that you make API request to that endpoint), then:

if ( ! is_admin() ) {
    // code here runs
}

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.

本文标签: Wordpress Rest filter for custom taxonomy gets applied to the admin area