2

This method for @SE How would I get a taxonomy/category list inside a Gutenberg block? does NOT work for custom taxonomies, e.g.:

wp.data.select('core').getEntityRecords('taxonomy', 'my-custom-taxonomy', {per_page: -1})

and only returns null, whereas the core category, i.e. "category", returns:

wp.data.select('core').getEntityRecords('taxonomy', 'category', {per_page: -1})

returns an entire array of taxonomy term data such as:

  • count
  • description
  • id
  • link
  • meta []
  • name
  • parent
  • slug
  • taxonomy etc.

The official docs at: https://developer.wordpress.org/block-editor/data/data-core/#getEntityRecords are woefully lacking, and whilst there are references to the "getEntityRecords" in GitHub at e.g.: https://github.com/WordPress/gutenberg/blob/04e142e9cbd06a45c4ea297ec573d389955c13be/packages/core-data/src/entities.js there is little in the way of documentation to help!

So here is the question:

How can you return the list of custom taxonomies using the wp.data.select('core').getEntityRecords method?

OR do you need to create a custom store, or use the wp.data Backbone JS?

1

ANSWER UPDATE: IF you have a large number of terms within your taxonomy, i.e. more than 100, you will need to add a {per_page: 100} args limiter in order NOT to get an empty array back, as you will if you try getting ALL results back, i.e. per_page: -1

so you will need something like:

wp.data.select('core').getEntityRecords('taxonomy', 'my-custom-taxonomy', {per_page: 100})

and that is the maximum on a single API call, although you can then append a "paging" arg, e.g.:

{per_page: 100, page: 2}

  • As you mentioned below, the initial call in Chrome at least returns an empty array - on 2nd request the test taxonomy did indeed return the expected array, even with the "per_page: -1" arg set - that seems to return an empty array when there are more than 100 results, although there is no warning / error message – Digitalle Nov 11 '19 at 17:31
  • I think the re-posted answer can help you better understand the empty array thing. I also rolled back your answer to your previous revision, but I'm keeping the +1 because the answer does have a valid point there. :) – Sally CJ Nov 12 '19 at 5:42
1
  1. The getEntityRecords() method uses the REST API, so make sure the taxonomy is enabled for the REST API. You can enable it via the show_in_rest parameter when registering the taxonomy using register_taxonomy().

  2. With getEntityRecords(), if you set per_page to -1, it will actually be changed to 100, which as you've figured it out, is the max number of results returned for a single API request (details here). Hence that's why per_page: -1 should work with getEntityRecords().

  3. Any AJAX/remote requests would not give you immediate results and we would need to wait a moment until the browser receives the response from the server. So this is probably the actual reason to why you're not getting any results immediately upon first call to getEntityRecords().

With that said, on subsequent calls (for the same query), you should get immediate results because getEntityRecords() cache the results (for performance reasons — you wouldn't want each call to getEntityRecords() takes several seconds to give you the results, would you?).

So try:

  1. wp.apiFetch() which is used by getEntityRecords(): Both these work; apiFetch() makes request to http://example.com/wp-json/wp/v2/your_tax?per_page=100:

    wp.apiFetch( { path: '/wp/v2/your_tax?per_page=-1' } )
        // 'terms' contains valid term objects
        .then( terms => console.log( terms ) );
    
    wp.apiFetch( { path: '/wp/v2/your_tax?per_page=100' } )
        // 'terms' contains valid term objects
        .then( terms => console.log( terms ) );
    
  2. fetch() which is used internally by wp.apiFetch():

    fetch( '/wp-json/wp/v2/your_tax?per_page=-1' )
        .then( res => res.json() )
        // 'terms' contains an error about per_page should be between 1 and 100
        .then( terms => console.log( terms ) );
    
    fetch( '/wp-json/wp/v2/your_tax?per_page=100' )
        .then( res => res.json() )
        // 'terms' contains valid term objects
        .then( terms => console.log( terms ) );
    

So if you make manual requests to /wp-json/wp/v2/your_tax (i.e. a taxonomy terms REST API), then you should not set the per_page to -1. But with wp.apiFetch() and functions which uses apiFetch() like getEntityRecords(), you can use that -1 although you should not

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.