I’m trying to write a new handler
.
I have a Custom Taxonomy for Posts called Folder.
I want to fetch all the posts with one fetch in multiple folders.
I have the ID’s of the folders, like: const folderIds = '7,10,66,123'
. So I want to be able to make a fetch like:
actions.source.fetch("/folders/" + folderIds);
And then get info about the Folders (an ACF field), and get all the posts in a Folder.
I created a handler, and I could get all the posts in all the folders, and get the ACF field of the folders, but I could not get the posts in a specific folder.
My handler looks like this, at the moment:
const FoldersHandler = {
pattern: "/folders/:ids",
priority: 1,
func: async ({ route, params, state, libraries }) => {
const { ids } = params;
//this will create a request like: https://wp.szinhaz.online/wp-json/wp/v2/posts?_embed=true&folder=969,23,22,1035,25,26,27,24,371&per_page=100
const postResponse = await libraries.source.api.get({
endpoint: "posts",
params: {
folder: ids,
per_page: 100,
_embed: true
}
});
const posts = await libraries.source.populate({state, response: postResponse});
//if I console.log(posts), I see that a post contains only id, link, and type
posts.map(post =>{
Object.assign(state.source.data[post.link],{
id: post.id,
type: post.type
});
});
//this will create a request like: https://wp.szinhaz.online/wp-json/wp/v2/folder?head_tags=false&include=969,23,22,1035,25,26,27,24,371&per_page=100
const folderResponse = await libraries.source.api.get({
endpoint: "folder",
params: {
include: ids,
per_page: 100,
head_tags: false
}
});
const folders = await libraries.source.populate({ state, response: folderResponse });
//if I console.log(folders), I see that a folder has an id, link and type: undefined
folders.map(folder => {
Object.assign(state.source.data[folder.link],{
id: folder.id,
taxonomy: "folder",
isArchive: true,
isTaxonomy: true,
isFolder: true
});
});
}
};
After this, in frontity.state.source.folder
I see all the folders I need, but they don’t have an items field, so I don’t know, what posts are in this folder.
In frontity.state.source.post
I see all the posts I should, and they have a folder property, with an array of id’s of the folders.
In frontity.state.source.data
I see the posts and the folders, and by both of them isFetching
and isReady
are false
.
How should I assign the posts to a folder?