I'm working on a Gutenberg block that will be a dropdown menu of all posts on the site. I used @wordpress/create-block
(version 4.4.0) for the scaffolding. When I try to use the useSelect
function, it get "This block has encountered an error and cannot be previewed." in addition to an error:
I'm trying to use code that was previously successful, although that code did not use the @wordpress/create-block
scaffolding (I built everything from scratch and the @wordpress/scripts
npm).
Here's my code:
import {useSelect} from "@wordpress/data"
export default function Edit() {
const allCPTposts = useSelect(select => {
return select("core").getEntityRecords("postType", "post", {per_page: -1})
})
if(allCPTposts == undefined){
return <p>Loading Posts...</p>
}
return (
<p { ...useBlockProps() }>
{ __( 'Dropdown Menu – hello from the editor!', 'dropdown' ) }
</p>
);
}
I'm not able to even work on the JSX for the actual dropdown menu because of this error.
Any ideas? Thank you in advance!
useBlockProps()
is the culprit here. If you move that above the conditional return, does it work again?per_page: -1
which is not possible, and if it were possible would be dangerous. The REST API caps requests at 100 posts for performance and scaling reasons. Arguably you should also be doing this in server side queries but there is no cap there, so while you can try to use-1
, you will never get all posts unless there are less than 100. The API returns 10 per page by default