5

I'm working on a plugin that defined a custom block for the Gutenberg/Block Editor. The block shows a select box of posts. When you select one, I create HTML with some of the selected post's fields, such as title, date, etc.

When I query my posts in the edit method, my code correctly produces a list of pages (in my case, a custom post type named product), but custom fields, which this post type has, are not included.

var pages = select('core').getEntityRecords('postType', 'product', { per_page: -1 });

I'm trying to find documentation on getEntityRecords query parameters, but there seems to be none. Is it possible to include custom fields in the query? You would think this should be explained on https://wordpress.org/gutenberg/handbook/designers-developers/developers/data/data-core/#getentityrecords

My only other idea was: when the block is saved ("during" the save method), is it possible to do another query that just selects the single post by ID and retrieves its data, maybe including custom fields? It seems I can't query anything during save.

4

Gutenberg documentation is not finished yet, but there are some unreleased commits in the official Gutenberg Repo. This could help: https://github.com/WordPress/gutenberg/blob/b7ad77d15f32ca234ff2f3df4994e47a5cf2e6d7/packages/editor/src/components/page-attributes/README.md

[...]
    var query = {
            per_page: -1,
            exclude: postId,
            parent_exclude: postId,
            orderby: 'menu_order',
            order: 'asc',
            status: 'publish,future,draft,pending,private',
        };
        return {
            parentItems: isHierarchical ?
                selectCore.getEntityRecords( 'postType', postTypeSlug, query ) :
                []
        };
[...]
4

If you look into the source code for getEntityRecords, you'll see that the the core/data library creates entities using the Wordpress API in entities.js.

So you can use any parameter available in the REST API. Here are options for posts:

context
page
per_page
search
after
author
author_exclude
before
exclude
include
offset
order
orderby
slug
status
categories
categories_exclude
tags
tags_exclude
sticky

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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