Closed kevinfodness closed 1 year ago
Another possible modification would be to restrict the post picker to picking only one post at a time. MediaUpload allows for multiselect, but I think in most cases the PostPicker is used for selecting one post at a time, and having to extract the ID from an array of post objects in the onSelect
callback isn't intuitive, especially when selecting just one post. From an editorial experience, it's likely also preferable to have one component responsible for each post selection to make it easier to pick a new post or reset the selection.
Another possible feature would be the ability to override the url of the endpoint used. If, for some reason, you didn't want to post to /wp-json/wp/v2/search?include=
but you could provide another endpoint that behaved in the same way (accepted the same parameters, returned the same data format), then you could pass a custom url. The default would be to use the search url if nothing is provided.
Some real world examples would be:
As someone who has struggled with the inherent complexities of the post selector, I love this enhancement request. Big plus 1 to restricting the PostPicker to 1 selection at a time. Not only would that reduce technical complexity, I agree that it also makes more editorial sense.
In addition to searching, we've had a request for showing the most recent posts. Maybe this can all be done in a modal similar to the media modal - show most recent by default with a search field as well.
Summarizing the discussion above, we should have v2 only select one post. I'll update the issue description accordingly.
Adding another request here so it doesn't get lost - please include the post type alongside the post title in the results in some way.
Closing this since #133 was merged
Problem
We need to rethink the post selector. It was built at a time when REST API support for looking up a post by ID without knowing its type was not possible. Now that we can do that, we should prioritize saving just the post ID via the post selector and enabling a preview of the content based on just an ID as well. It should be significantly simpler to integrate a PostSelector than it is today.
Proposed Solution
Let's reconfigure the PostSelector component in the following ways:
PostPicker
to align the naming convention withImagePicker
,MediaPicker
, etc., and deprecate the existingPostSelector
component.subTypes
should be renamed toallowedTypes
selected
should be renamed tovalue
and should accept a number representing the selected post IDlabel
should be renamed totitle
render
prop should be added that will receive post objects for the selected posts and will allow for custom rendering of previews of these posts.Example Usage
The value of
post
in the render callback will be obtained by getting the post by ID using the search REST API endpoint and using theinclude
query parameter, like:/wp-json/wp/v2/search?include=1234
. Once we have the response from the search endpoint for the selected post, we will know the type and subtype, and can make a subsequent query for the full post object. For performance reasons, we should cache the association of IDs to subtypes so we don't have to look it up more than necessary, and we can useuseSelect
to fetch entities once we know their type by doingselect('core').getEntityRecord('postType', postType, postId)
.