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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
}
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0