Fetching other posttypes on the frontpage

Hi!

For 2 weeks I have been working with frontity and learning how to works. So far, so good. I am a very experienced Wordpress developer, but not very experienced working with Frontity or React, in fact, this is the first time I’m doing a react project.

I’m currently developing a theme based on the awsf1, which was a really good base to learn from (IMO).

Right now I’m currently stuck with the frontpage because I want to query 1 custom post type and 1 normal post type and list everything. But the data wont fetch, which messes up the map-function :slight_smile:

Here’s my snippet for fetching the 2 data sources:

const HomePage = ({ state, actions, libraries }) => {
  // Get information about the current URL.
  const data = state.source.get(state.router.link);
  // Get the data of the post.
  const homepage = state.source[data.type][data.id];

  // Get the html2react component.
  const Html2React = libraries.html2react.Component;
  //const BannerSlider = homepage.acf.banner_slider; 
  useEffect(() => {
    actions.source.fetch("/");
    List.preload();
  }, []);

  **const blogposts = state.source.get('/posts');**
  **const reviews = state.source.get('/prp_reviews');**

I’ve bolded the 2 data sources. I would really appreciate someone pointing me in the right direction.

Best regards,
Marten

Hi @marten

Welcome to the community. We’re delighted that you’re trying out Frontity.

You shouldn’t need to use useEffect to get the homepage content, i.e. "/" as that content should already be in the state because of your second line, namely const data = state.source.get(state.router.link).

But you would probably need to use useEffect to fetch the CPT, and the posts as it seems you’re creating a custom homepage and not using a standard post listing.

You’re using state.source.get('/prp_reviews') and state.source.get('/posts') but they won’t be in the state unless they’ve already been fetched.

Another way would be to use a beforeSSR action.

I hope the above helps, but if you’re still having issues then it would be helpful for us if you can provide a link to a repo so that we can see your code.

Thanks for pointing me in the right directing. Unlike WordPress, it seems like Frontity (or react) likes to have the code organized properly so I had to create a separate function/module which I then called from the homepage template.