本文档介绍了 WordPress 核心数据存储(core-data)中的选择器和操作,用于管理实体记录如文章类型、分类法、用户等。这些工具基于 getEntityRecord 和 getEntityRecords 封装,提供用户友好的接口,支持数据查询、编辑和状态管理。
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
const postType = useSelect(
( select ) => select( coreDataStore ).getPostType( 'post' )
);Namespace: core.
There are a number of user-friendly selectors that are wrappers of the more generic getEntityRecord and getEntityRecords that can be used to retrieve information for the various entities.
Returns the information for a given post type.
Usage
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
const postType = useSelect(
( select ) => select( coreDataStore ).getPostType( 'post' )
// Equivalent to: select( coreDataStore ).getEntityRecord( 'root', 'postType', 'post' )
);
Parameters
stringReturns
EntityRecord | undefined: Record.Returns the information for post types.
Usage
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
const postTypes = useSelect( ( select ) => {
return select( coreDataStore ).getPostTypes( { per_page: 4 } );
// Equivalent to:
// select( coreDataStore ).getEntityRecords( 'root', 'postType', { per_page: 4 } );
} );
Parameters
GetRecordsHttpQuery: Optional terms query. If requesting specific fields, fields must always include the ID. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available for “List [Entity kind]s”.Returns
EntityRecord[] | null: Records.Returns information for a given taxonomy.
Usage
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
const taxonomy = useSelect( ( select ) => {
return select( coreDataStore ).getTaxonomy( 'category' );
// Equivalent to:
// select( coreDataStore ).getEntityRecord( 'root', 'taxonomy', 'category' );
} );
Parameters
stringReturns
EntityRecord | undefined: Record.Returns information for taxonomies.
Usage
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
const taxonomies = useSelect( ( select ) => {
return select( coreDataStore ).getTaxonomies( { type: 'post' } );
// Equivalent to:
// select( coreDataStore ).getEntityRecords( 'root', 'taxonomy', { type: 'post' } );
} );
Parameters
GetRecordsHttpQuery: Optional terms query. If requesting specific fields, fields must always include the ID. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available for “List [Entity kind]s”.Returns
EntityRecord[] | null: Records.Returns whether the current user can perform the given action on the given REST resource.
Calling this may trigger an OPTIONS request to the REST API via the canUser() resolver.
https://developer.wordpress.org/rest-api/reference/
Parameters
State: Data state.string: Action to check. One of: ‘create’, ‘read’, ‘update’, ‘delete’.string | EntityResource: Entity resource to check. Accepts entity object { kind: 'postType', name: 'attachment', id: 1 } or REST base as a string – media.EntityRecordKey: Optional ID of the rest resource to check.Returns
boolean | undefined: Whether or not the user can perform the action, or undefined if the OPTIONS request is still being made.Returns whether the current user can edit the given entity.
Calling this may trigger an OPTIONS request to the REST API via the canUser() resolver.
https://developer.wordpress.org/rest-api/reference/
Parameters
State: Data state.string: Entity kind.string: Entity name.EntityRecordKey: Record’s id.Returns
boolean | undefined: Whether or not the user can edit, or undefined if the OPTIONS request is still being made.Deprecated since 11.3. Callers should use
select( 'core' ).getUsers({ who: 'authors' })instead.
Returns all available authors.
Parameters
State: Data state.GetRecordsHttpQuery: Optional object of query parameters to include with request. For valid query parameters see the Users page in the REST API Handbook and see the arguments for List Users and Retrieve a User.Returns
ET.User[]: Authors list.Returns the autosave for the post and author.
Parameters
State: State tree.string: The type of the parent post.EntityRecordKey: The id of the parent post.EntityRecordKey: The id of the author.Returns
EntityRecord | undefined: The autosave for the post and author.Returns the latest autosaves for the post.
May return multiple autosaves since the backend stores one autosave per author for each post.
Parameters
State: State tree.string: The type of the parent post.EntityRecordKey: The id of the parent post.Returns
Array< any > | undefined: An array of autosaves for the post, or undefined if there is none.Retrieve the list of registered block pattern categories.
Parameters
State: Data state.Returns
Array< any >: Block pattern category list.Retrieve the list of registered block patterns.
Parameters
State: Data state.Returns
Array< any >: Block pattern list.Return the current theme.
Parameters
State: Data state.Returns
any: The current theme.Deprecated since WordPress 6.5.0. Callers should use
select( 'core' ).getRevisions( 'root', 'globalStyles', ${ recordKey } )instead, whererecordKeyis the id of the global styles parent post.
Returns the revisions of the current global styles theme.
Parameters
State: Data state.Returns
Array< object > | null: The current global styles.Returns the current user.
Parameters
State: Data state.Returns
ET.User< 'view' >: Current user object.Returns the default template use to render a given query.
Parameters
State: Data state.TemplateQuery: Query.Returns
string: The default template id for the given query.Returns the specified entity record, merged with its edits.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
ET.Updatable< EntityRecord > | false: The entity record, merged with its edits.Returns the embed preview for the given URL.
Parameters
State: Data state.string: Embedded URL.Returns
any: Undefined if the preview has not been fetched, otherwise, the preview fetched from the embed preview API.Deprecated since WordPress 6.0. Use getEntitiesConfig instead
Returns the loaded entities for the given kind.
Parameters
State: Data state.string: Entity kind.Returns
Array< any >: Array of entities with config matching kind.Returns the loaded entities for the given kind.
Parameters
State: Data state.string: Entity kind.Returns
Array< any >: Array of entities with config matching kind.Deprecated since WordPress 6.0. Use getEntityConfig instead
Returns the entity config given its kind and name.
Parameters
State: Data state.string: Entity kind.string: Entity name.Returns
any: Entity configReturns the entity config given its kind and name.
Parameters
State: Data state.string: Entity kind.string: Entity name.Returns
any: Entity configReturns the Entity’s record object by key. Returns null if the value is not yet received, undefined if the value entity is known to not exist, or the entity object if it exists and is received.
Parameters
State: State treestring: Entity kind.string: Entity name.EntityRecordKey: Optional record’s key. If requesting a global record (e.g. site settings), the key can be omitted. If requesting a specific item, the key must always be included.GetRecordsHttpQuery: Optional query. If requesting specific fields, fields must always include the ID. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available “Retrieve a [Entity kind]”.Returns
EntityRecord | undefined: Record.Returns the specified entity record’s edits.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
Optional< any >: The entity record’s edits.Returns the specified entity record’s non transient edits.
Transient edits don’t create an undo level, and are not considered for change detection. They are defined in the entity’s config.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
Optional< any >: The entity record’s non transient edits.Returns the Entity’s records.
Parameters
State: State treestring: Entity kind.string: Entity name.GetRecordsHttpQuery: Optional terms query. If requesting specific fields, fields must always include the ID. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available for “List [Entity kind]s”.Returns
EntityRecord[] | null: Records.Returns the Entity’s total available records for a given query (ignoring pagination).
Parameters
State: State treestring: Entity kind.string: Entity name.GetRecordsHttpQuery: Optional terms query. If requesting specific fields, fields must always include the ID. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available for “List [Entity kind]s”.Returns
number | null: number | null.Returns the number of available pages for the given query.
Parameters
State: State treestring: Entity kind.string: Entity name.GetRecordsHttpQuery: Optional terms query. If requesting specific fields, fields must always include the ID. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available for “List [Entity kind]s”.Returns
number | null: number | null.Returns the specified entity record’s last delete error.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
any: The entity record’s save error.Returns the specified entity record’s last save error.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
any: The entity record’s save error.Returns the entity’s record object by key, with its attributes mapped to their raw values.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record’s key.Returns
EntityRecord | undefined: Object with the entity’s raw attributes.Deprecated since 6.3
Returns the next edit from the current undo offset for the entity records edits history, if any.
Parameters
State: State tree.Returns
Optional< any >: The edit.Returns a new reference when edited values have changed. This is useful in inferring where an edit has been made between states by comparison of the return values using strict equality.
Usage
const hasEditOccurred = (
getReferenceByDistinctEdits( beforeState ) !==
getReferenceByDistinctEdits( afterState )
);
Parameters
Returns
Returns a single, specific revision of a parent entity.
Parameters
State: State treestring: Entity kind.string: Entity name.EntityRecordKey: The key of the entity record whose revisions you want to fetch.EntityRecordKey: The revision’s key.GetRecordsHttpQuery: Optional query. If requesting specific fields, fields must always include the ID. For valid query parameters see revisions schema in the REST API Handbook. Then see the arguments available “Retrieve a [entity kind]”.Returns
RevisionRecord | Record< PropertyKey, never > | undefined: Record.Returns an entity’s revisions.
Parameters
State: State treestring: Entity kind.string: Entity name.EntityRecordKey: The key of the entity record whose revisions you want to fetch.GetRecordsHttpQuery: Optional query. If requesting specific fields, fields must always include the ID. For valid query parameters see revisions schema in the REST API Handbook. Then see the arguments available “Retrieve a [Entity kind]”.Returns
RevisionRecord[] | null: Record.Returns the current sync connection status across all entities. Prioritizes disconnected states, then connecting, then connected.
Parameters
State: Data state.Returns
ConnectionStatus | undefined: The current sync connection state, prioritized by importance.Return theme supports data in the index.
Parameters
State: Data state.Returns
any: Index data.Deprecated since 6.3
Returns the previous edit from the current undo offset for the entity records edits history, if any.
Parameters
State: State tree.Returns
Optional< any >: The edit.Retrieve the registered user pattern categories.
Parameters
State: Data state.Returns
Array< UserPatternCategory >: User patterns category array.Returns all the users returned by a query ID.
Parameters
State: Data state.string: Query ID.Returns
ET.User< 'edit' >[]: Users list.Returns true if the specified entity record has edits, and false otherwise.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
boolean: Whether the entity record has edits or not.Returns true if a record has been received for the given set of parameters, or false otherwise.
Note: This action does not trigger a request for the entity record from the API if it’s not available in the local state.
Parameters
State: State treestring: Entity kind.string: Entity name.EntityRecordKey: Record’s key.GetRecordsHttpQuery: Optional query.Returns
boolean: Whether an entity record has been received.Returns true if records have been received for the given set of parameters, or false otherwise.
Parameters
State: State treestring: Entity kind.string: Entity name.GetRecordsHttpQuery: Optional terms query. For valid query parameters see the Reference in the REST API Handbook and select the entity kind. Then see the arguments available for “List [Entity kind]s”.Returns
boolean: Whether entity records have been received.Returns true if the REST request for autosaves has completed.
Parameters
State: State tree.string: The type of the parent post.EntityRecordKey: The id of the parent post.Returns
boolean: True if the REST request was completed. False otherwise.Returns true if there is a next edit from the current undo offset for the entity records edits history, and false otherwise.
Parameters
State: State tree.Returns
boolean: Whether there is a next edit or not.Returns true if a revision has been received for the given set of parameters, or false otherwise.
Note: This does not trigger a request for the revision from the API if it’s not available in the local state.
Parameters
State: State treestring: Entity kind.string: Entity name.EntityRecordKey: The key of the entity record whose revision you want to check.EntityRecordKey: The revision’s key.GetRecordsHttpQuery: Optional query.Returns
boolean: Whether a revision has been received.Returns true if there is a previous edit from the current undo offset for the entity records edits history, and false otherwise.
Parameters
State: State tree.Returns
boolean: Whether there is a previous edit or not.Returns true if the specified entity record is autosaving, and false otherwise.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
boolean: Whether the entity record is autosaving or not.Returns true if the specified entity record is deleting, and false otherwise.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
boolean: Whether the entity record is deleting or not.Determines if the returned preview is an oEmbed link fallback.
WordPress can be configured to return a simple link to a URL if it is not embeddable. We need to be able to determine if a URL is embeddable or not, based on what we get back from the oEmbed preview API.
Parameters
State: Data state.string: Embedded URL.Returns
boolean: Is the preview for the URL an oEmbed link fallback.Returns true if a request is in progress for embed preview data, or false otherwise.
Parameters
State: Data state.string: URL the preview would be for.Returns
boolean: Whether a request is in progress for an embed preview.Returns true if the specified entity record is saving, and false otherwise.
Parameters
State: State tree.string: Entity kind.string: Entity name.EntityRecordKey: Record ID.Returns
boolean: Whether the entity record is saving or not.Returns an action object used in adding new entities.
Parameters
Array: Entities received.Returns
Object: Action object.Action triggered to clear all edits from an entity record.
Parameters
string: Kind of the entity.string: Name of the entity.number|string: Record ID of the entity record.Returns
Object: Action object.Action triggered to delete an entity record.
Parameters
string: Kind of the deleted entity.string: Name of the deleted entity.number|string: Record ID of the deleted entity.?Object: Special query parameters for the DELETE API call.[Object]: Delete options.[Function]: Internal use only. Function to call instead of apiFetch(). Must return a promise.[boolean]: If false, this action suppresses all the exceptions. Defaults to false.Returns an action object that triggers an edit to an entity record.
Parameters
string: Kind of the edited entity record.string: Name of the edited entity record.number|string: Record ID of the edited entity record.Object: The edits.Object: Options for the edit.[boolean]: Whether to ignore the edit in undo history or not.Returns
Object: Action object.Returns an action object used to set the template for a given query.
Parameters
Object: The lookup query.string: The resolved template id.Returns
Object: Action object.Returns an action object used in signalling that entity records have been received.
Parameters
string: Kind of the received entity record.string: Name of the received entity record.Array|Object: Records received.?Object: Query Object.?boolean: Should invalidate query caches.?Object: Edits to reset.?Object: Meta information about pagination.Returns
Object: Action object.Returns an action object signalling that the fallback Navigation Menu id has been received.
Parameters
integer: the id of the fallback Navigation MenuReturns
Object: Action object.Action triggered to receive revision items.
Parameters
string: Kind of the received entity record revisions.string: Name of the received entity record revisions.number|string: The key of the entity record whose revisions you want to fetch.Array|Object: Revisions received.?Object: Query Object.?boolean: Should invalidate query caches.?Object: Meta information about pagination.Deprecated since WP 5.9, this is not useful anymore, use the selector directly.
Returns an action object used in signalling that the index has been received.
Returns
Object: Action object.Deprecated since WP 5.9, use receiveUserPermission instead.
Returns an action object used in signalling that Upload permissions have been received.
Parameters
boolean: Does the user have permission to upload files?Returns
Object: Action object.Action triggered to redo the last undone edit to an entity record, if any.
Action triggered to save an entity record’s edits.
Parameters
string: Kind of the entity.string: Name of the entity.Object: ID of the record.Object=: Saving options.Action triggered to save an entity record.
Parameters
string: Kind of the received entity.string: Name of the received entity.Object: Record to be saved.Object: Saving options.[boolean]: Whether this is an autosave.[Function]: Internal use only. Function to call instead of apiFetch(). Must return a promise.[boolean]: If false, this action suppresses all the exceptions. Defaults to false.Returns an action object used to set the sync connection status for an entity or collection.
Parameters
string: Kind of the entity.string: Name of the entity.number|string|null: The entity key, or null for collections.Object|null: The connection state object or null on unload.Returns
Object: Action object.Action triggered to undo the last edit to an entity record, if any.