Search code examples
wordpress-gutenberggutenberg-blocks

Wordpress Gutenberg withSelect get list of post types


I am using Gutenberg's withSelect package to try to get all of the post types belonging to the site. Here is my code:

export default withSelect( select => {
    // shorthand
    const { getEntityRecords } = select( 'core' );

    return {
        typesList: getEntityRecords( 'types', '' ),
    };
} )( SearchEdit );

But I am not getting anything back. I also found the documentation for withSelect very sparse so if anyone has any further information that would be great and I will try to update the docs.


Solution

  • Know it has been a while since this was asked, but I had this come up today so here is an answer. There is a method called getPostTypes(), see the example below:

    export default withSelect( select => {
        const {getPostTypes} = select('core);
    
        return {
            typesList: getPostTypes(),
        }
    } )( YourEdit );
    

    Note: getPostTypes() will return an array of post type objects. If you want just the names, you will need to process the results into the desired format.