Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getEntityRecords() for 'attachment' post type no longer works with Wordpress 5.6 #27771

Closed
korynorthrop opened this issue Dec 16, 2020 · 5 comments · Fixed by #40570
Closed
Assignees
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability Good First Issue An issue that's suitable for someone looking to contribute for the first time [Package] Data /packages/data [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@korynorthrop
Copy link

korynorthrop commented Dec 16, 2020

Describe the bug
I have been making use of getEntityRecords() to fetch image data to display in some custom Gutenberg blocks. They worked fine until I upgraded to Wordpress 5.6. Now when I make calls with getEntityRecords() I get the following errors:
"Uncaught TypeError: fe(...) is null"
"Uncaught (in promise) TypeError: fe(...) is null"

However, I can successfully fetch image data by using getMedia(). This was the behavior I experienced on my live and staging sites. I deactivated all plugins and changed to Twentytwentyone theme and still had the issue. So I installed a fresh copy of Wordpress locally and went through the following steps, which produced the error as well.

To reproduce
Steps to reproduce the behavior:

  1. Make a clean Wordpress installation
  2. Upload an image to the media library and take note of it's post_id (let's say it's 6)
  3. Open the page editor and the Javascript console then type in wp.data.select('core').getEntityRecords('postType', 'attachment', { include: 6 })
  4. See error
  5. Instead type in wp.data.select('core').getMedia(6) (you'll likely get 'undefined' the first time while it's fetching the media item, but if you re-enter you should be able to see the details for that image)

Expected behavior
I expected to be able to continue using getEntityRecords() within my custom blocks like I did before. I noticed that a similar error would occur fetching pages (e.g. wp.data.select('core').getEntityRecords('postType', 'page', { include: 3912 } )) but if I passed the post_id as a string in the query parameter it works (e.g. wp.data.select('core').getEntityRecords('postType', 'page', { include: '3912' } ). However, passing the attachment ID as a string always returns null

Screenshots
Firefox console error:
Screen Shot 2020-12-16 at 13 31 47

Chrome console error, slightly different message:
Screen Shot 2020-12-16 at 13 54 12

This screenshot shows the successful getMedia() call for the same image id:
Screen Shot 2020-12-16 at 13 54 43

Editor version (please complete the following information):

  • WordPress version: 5.6
  • Default block editor
  • OSX 10.15.7
  • Firefox 83 and Chrome 83
@talldan talldan added [Package] Data /packages/data [Type] Bug An existing feature does not function as intended Backwards Compatibility Issues or PRs that impact backwards compatability labels Dec 17, 2020
@talldan
Copy link
Contributor

talldan commented Dec 17, 2020

Thanks for reporting. I got different results. Using a string works:

wp.data.select('core').getEntityRecords('postType', 'attachment', { include: '12' })

This returned null first, and then the second time the attachment data after the request had resolved.

It seems like the intention is that you should use an array:

wp.data.select('core').getEntityRecords('postType', 'attachment', { include: [ 12 ] })

The idea seems to be that a string can support multiple values like this:

'12,13,14'

And an array can support multiple values like this:

[ 12, 13, 14 ]

And both can also support a single value.

I'll label as a bug, as it seems like a backwards compatibility issue.

@talldan talldan added the Good First Issue An issue that's suitable for someone looking to contribute for the first time label Dec 17, 2020
@korynorthrop
Copy link
Author

Thanks, on my fresh install I am able to get the string working as well as the array. For some reason on our production and staging sites both of those options consistently produce null. So, I'm not sure what to make of that, I'll have to try disabling plugins and changing the theme again to see if there are any conflicts. Do you have any theories on what would lead to a consistent null response for a valid ID of an image that exists in the Media Library?

@korynorthrop
Copy link
Author

@talldan I've chased the issue down to a conflict with the WP REST Cache plugin. I had suspected earlier that this plugin might be a partial cause, but I was clearing its cache while testing, assuming that would alleviate any potential conflicts with these getEntityRecords() calls. However, it seems that when WP REST Cache is active it cause all of the getEntityRecords responses to come back null. I'll report it to the plugin author(s).

@AdamSoucie
Copy link

I can confirm this is still an issue in 5.6.1, at least with pages. Using the "include" parameter fails, provided the value for the ID is passed as an integer. When I pass the value in as a string, as @talldan did in their example, it worked as expected. In 5.5.3, I didn't need to convert to a string first.

Additional info:
Unlike @korynorthrop, I am not running any caching plugins, so while that may be causing a separate issue, that is not the root cause of this problem.

@mahammedzkhan
Copy link

mahammedzkhan commented Apr 28, 2021

I'm having the same issue with WordPress 5.7.1, using an integer for include doesn't work but a string works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability Good First Issue An issue that's suitable for someone looking to contribute for the first time [Package] Data /packages/data [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants