4

As the title states https://developer.wordpress.org/block-editor/data/data-core/#getEntityRecords doesn't allow an array of post types or any sort of delimited types. WP_Query normally accepts 'any' but that doesn't work any suggestion for a JSX novice please. Ive checked my local endpoint REST endpoint /wp/v2/types for any clues and this just shows me all my existing post types i can filter by but no indication how to filter by multiple post types. I currently have this.

const { getEntityRecords } = select( 'core' );
return {
    suggestions: getEntityRecords( 'posts', '', { per_page: -1, search: ownProps.value } ), 
    onChange: ownProps.onChange,
    value: ownProps.value
}

2 Answers 2

1

I found a workaround for this by

  1. creating a function to query all post types in PHP
  2. passing this data as a var via a localized script into the block editor
  3. mapping this var and calling the getEntityRecords for each one
  4. pushing that into an array to use as needed

Code snippets below.

PHP

function get_all_post_types() {

    $post_types_arr[];
    $post_types = get_post_types(array(
       'public' => true,
    ), 'objects');

   // sort post types by name
   uasort($post_types, function($a, $b) {
       return strcmp($a->labels->name, $b->labels->name);
   });

   foreach ($post_types as $post_type) {
       array_push($post_types_arr, $post_type);
   }

   return $post_types_arr;
}

add_action( 'init', 'include_posttype_array_in_blocks_on_init' );
function include_posttype_array_in_blocks_on_init() {

   $asset_file = include( __DIR__ . 'build/index.asset.php' );

   wp_register_script(
       'posttypes-for-blocks',
       __DIR__ . '/build/index.js',
       $asset_file['dependencies'],
       $asset_file['version']
   );

   wp_localize_script(
      'posttypes-for-blocks',
      'postTypeVars',
      array(
         'postTypes' => get_all_post_types()
      )
   ); 
}

In the block edit.js file

let data = [];
const postTypes = postTypeVars.postTypes;
const posts = postTypes.map( ( postType ) => {
        const post = useSelect( ( select ) => {
            return select( 'core' ).getEntityRecords( 'postType', postType, args );
        });
        data.push( post );
    } );

// flatten the array into one array
data = data.flat();

Now you can do whatever you want with the post data.

1
  • You can get all the post types with select( 'core' ).getPostTypes( { per_page: -1 })
    – rgdigi
    Sep 19, 2023 at 13:47
1

It is not possible to query multiple post types simultaneously using the getEntityRecords() selector. This is because the WordPress REST API is not built around the same ideology as the core WordPress back-end.

All data-types being "posts" in the traditional PHP APIs is the product of the somewhat controversial underlying database implementation, while the REST API was built to be insulated from the database implementation, and treats each post type as it's own "resource" in isolation for the most part.

In your case, searching the relevant record-types individually is probably the best solution. If you really do wish to search all post types which are exposed over the REST API, you can use the getPostTypes() selector from the core data store in order to retrieve a list of types.

Alternatively, the REST API does expose a search endpoint. While not presently very clearly documented in the REST API Handbook, this endpoint's subtype parameter does accept an array of post types. This could be queried using the @wordpress/api-fetch package, but it's worth noting that the functionality and caching would not automatically leverage Gutenberg's data stores - it may be necessary to implement a custom data-store to most efficiently implement your search functionality (prevent identical repeated requests to the REST API per render, automatically trigger re-renders for selected data, etc.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.