Retrieve posts of custom post type on homepage

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 @sean.doldersum

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:

Bildschirmfoto 2020-08-06 um 11.46.01

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.

1 Like

Ok that actually worked! Thanks for your help :slight_smile:

1 Like