Wordpress Gutenberg withSelect get list of post types
Asked Answered
S

1

5

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.

Subtype answered 21/11, 2018 at 1:25 Comment(0)
B
6

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.

Brechtel answered 2/1, 2019 at 19:9 Comment(1)
@Subtype I inspected the results of select('core'). There are also methods for taxonomies and taxonomy terms among other useful items.Brechtel

© 2022 - 2024 — McMap. All rights reserved.