getEntityRecords/useEntityRecords: How to use CPT metadata?
I use getEntityRecords
as described in the official data API tutorial (https://learn.wordpress.org/lesson/retrieving-wordpress-data-records/).
I.e.:
const [searchTerm, setSearchTerm] = useState( '' );
const pages = useSelect( select => {
const query = {};
if ( searchTerm ) {
query.search = searchTerm;
}
return select( coreDataStore ).getEntityRecords( 'postType', 'myCustomPostType', query );
}, [searchTerm] );
This works fine and as expected. Now I want to adjust the query so that the posts’ meta can be used to reduce the query result.
I tried various approaches such as
const [searchTerm, setSearchTerm] = useState( '' );
const pages = useSelect( select => {
const query = {};
if ( searchTerm ) {
query.search = searchTerm;
}
if (postId) {
query.meta = [
{
myMetaKey: postId,
},
];
}
return select( coreDataStore ).getEntityRecords( 'postType', 'myCustomPostType', query );
}, [searchTerm] );
but to no avail.
As I understand the ‘getEntityRecords’ documentation:
query 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”.
querying the metadata should be possible.
I’m aware of meta_query with standard getEntityRecords possible? but am not fully sure if it relates to this problem.
Any help appreciated.
Leave an answer