Hi there,
For a project I’m doing i need to show 5-10 posts of a post type (vacancies) on the homepage. How can I retrieve these items and their data?
Thanks in advance!
Hi there,
For a project I’m doing i need to show 5-10 posts of a post type (vacancies) on the homepage. How can I retrieve these items and their data?
Thanks in advance!
Our documentation for setting up wp-source
to fetch custom post types is here.
Once you have configured frontity.settings.js
with the required info to fetch the post types then you will need to ‘pre-fetch’ them in your theme.
In your theme’s index.js
file you should add a beforeSSR
action to fetch the posts. Our documentation for beforeSSR
is here.
Hopefully that should be enough to get you going, but if you need further help please don’t hesitate to ask. A link to a repo with your code will be helpful if you have any further questions.
Hello there,
i did also run into issues fetching custom post types.
What i did by now:
in frontity.settings.js:
…
“source”: {
“api”: “http://websitename/frontity/wp-json”,
“postTypes”: [
{
“type”: “projekte”,
“endpoint”: “projekte”,
}
],
…
in Root.js:
useEffect(() => {
actions.source.fetch("/projekte/");
}, []);
console.log(state.source.get("/projekte/"));
what i receive:
it says that the custom post type is not defined. But it is actually available in the wp-json API enpoint.
Did i miss a thing?
Hi @pirmin.bahr
Try adding the archive
property to your postTypes
definition:
"postTypes": [
{
"type": "projekte",
"endpoint": "projekte",
"archive": <URL of CPT archive>
}
],
Let me know if this helps. See here for more info.
Ok that actually worked! Thanks for your help
Hello everyone,
I am having an issue retrieving my custom posts types. I got the data successfully following the demo here and also the documentation for custom post types you posted earlier.
However, the data that I get back is lacking information such as the title and other custom fields. I am using the beforeSSR action specified here.
This is the data that I get back:
I need to add more data, such as the post title and other custom metaboxes from advanced custom fields. Any help will be appreciated.
Hello,
I am going to reply to my own question. I took a closer look at the code from the example I linked in the question above (the custom post type frontity example). And I realized that I can get all the data I need by passing the name of the CPT and the id to state.source, as seen in the screenshot below.