Fix ExcludePostsControl fetching all posts#160
Conversation
Replace per_page: -1 with per_page: 25 and add search support in ExcludePostsControl, matching the existing IncludePostsControl pattern. This prevents endless paginated REST API calls that hang the editor on sites with many posts. Fixes: ryanwelcher#157
- Fetch saved exclude IDs separately via include param so saved selections are never dropped from the token field - Cache resolved posts in a ref so title lookups survive search input resets - Add search_columns: post_title so REST results match what FormTokenField filters client-side - Add _fields: id,title to keep responses lightweight - Set per_page: 10 to match IncludePostsControl
- debounce the search input - only call server when we have a search argument - limit search_column to title, and only return id and title - user friendly messages when searching instead of No items found with no spinner
- port UX improvements from PostIncludeControls
- both now use a common PostPickerControl - legacy support for ExcludePostsControl where the block attribute is an array of IDs
…y-title Fix/include posts search by title
…alues when called with the same state and parameters." warning
… returned from useSelect every time.
…y-title Refactor ExcludePostsControl and PostIncludesControl
…y-title fix JS linting errors
…y-title Addressing linting errors
ryanwelcher
left a comment
There was a problem hiding this comment.
Overall, this looks great. I need to test it to make sure it compatible with the old way of storing posts. I just have one real note to address. I am at WCEU right now so I won't be able to release this until I get back.
|
I was able to test the back compat and it seems to work fine. I'd love another set of eyes on this if possible @TrevorMills have you been able to test this on a site with a large data set? I think this is ready to go once the helper is moved out into a new file as per my comment - great work! |
@ryanwelcher Yes, we tested this with a data set of around 10K posts and it worked well! Your feedback is addressed in a PR on the downstream fork of this. |
Amazing! Once that's merged, I'll merge this and get it released once I am back from WCEU. Thanks for the great work here! |
Fixes #157
Fixes #142
Props to @trevormills-xwp for the PostPickerControl refactor, debounce, loading states, and backwards compat migration.
Problem
ExcludePostsControlcallsgetEntityRecordswithper_page: -1. The REST API middleware splits this into paginated requests of 100. On sites with thousands of posts, these requests pile up, hang the block editor, and block post saving and autosaves.Fix
Refactors both
ExcludePostsControlandPostIncludeControlsinto a sharedPostPickerControlcomponent that:per_page: -1withper_page: 10with debounced search-as-you-type (500ms)search_columns: post_titleso REST results match whatFormTokenFieldfilters client-side_fields: id,titleto keep responses lightweightSetto preventFormTokenFieldissuesBackwards compatibility:
exclude_postsattribute format changed from[id, id]to[{id, title}, ...]to matchinclude_postsuseSelect+useEffectExclude_Poststrait updated to handle numeric,{id, title}, and plain array formatsOther changes:
defined('ABSPATH') || exitchanged toifblock in PHP files for phpunit compatibilityTesting
exclude_posts(array of IDs) - confirm auto-migration to{id, title}format.wp/v2/postsrequests withper_page=10, not paginated chains.