✅ 1. Fetch Custom Post Type (product)
Just replace 'post' with 'product':
const { products, isLoading } = useSelect((select) => {
const query = {
per_page: 5,
_fields: ['id', 'title'],
}; return {
products: select('core').getEntityRecords('postType', 'product', query),
isLoading: !select('core').hasFinishedResolution(
'getEntityRecords',
['postType', 'product', query]
),
};
}, []);
✅ 2. Fetch Taxonomy (product_cat)
For taxonomies, entity type changes from 'postType' → 'taxonomy'
const { categories, isCatLoading } = useSelect((select) => {
const query = {
per_page: 10,
_fields: ['id', 'name'],
}; return {
categories: select('core').getEntityRecords('taxonomy', 'product_cat', query),
isCatLoading: !select('core').hasFinishedResolution(
'getEntityRecords',
['taxonomy', 'product_cat', query]
),
};
}, []);
🚀 3. Fetch BOTH (Best Practice – Single useSelect)
👉 This is what you should do in production (avoids multiple subscriptions)
const { products, categories, isLoading } = useSelect((select) => {
const productQuery = {
per_page: 5,
_fields: ['id', 'title'],
}; const categoryQuery = {
per_page: 10,
_fields: ['id', 'name'],
}; const core = select('core'); return {
products: core.getEntityRecords('postType', 'product', productQuery),
categories: core.getEntityRecords('taxonomy', 'product_cat', categoryQuery), isLoading:
!core.hasFinishedResolution('getEntityRecords', ['postType', 'product', productQuery]) ||
!core.hasFinishedResolution('getEntityRecords', ['taxonomy', 'product_cat', categoryQuery]),
};
}, []);
🔗 4. Filter Products by Category (Very Useful)
If you want products by category, pass taxonomy query:
const query = {
per_page: 5,
product_cat: 12, // category ID
};
👉 Full example:
products: core.getEntityRecords('postType', 'product', {
per_page: 5,
product_cat: 12,
})
⚠️ Important Notes
1. CPT must be exposed to REST API
When registering CPT:
'show_in_rest' => true,
2. Taxonomy must also support REST
'show_in_rest' => true,
3. _fields optimization
Be careful:
_fields: ['id', 'title']
- Works for posts
- For taxonomy → use
name, nottitle
🧠 Key Takeaways
- CPT →
'postType', 'product' - Taxonomy →
'taxonomy', 'product_cat' - Combine in single
useSelectfor performance - Use
hasFinishedResolutionfor loading state - Pass taxonomy slug in query to filter posts