最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What are all the query parameters for getEntityRecords?

programmeradmin3浏览0评论

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

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.

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/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.

Share Improve this question asked Feb 10, 2019 at 12:34 oelnaoelna 2211 gold badge2 silver badges5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

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

Gutenberg documentation is not finished yet, but there are some unreleased commits in the official Gutenberg Repo. This could help: https://github/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 ) :
                []
        };
[...]
发布评论

评论列表(0)

  1. 暂无评论