Ordering posts by author

I just started a project with the framework and I am not getting a couple of thing about calls.

What is the correct way of creating a call to get the lastest post or make a search in the blog?

Because if I define some params on state.route, every call is going to have those params, right? I only want the params in certain calls e.g. getting sticky posts or /posts?orderBy=date

And using post calls? I haven’t tried but I dont know where to start.

I have read the docs a couple of times and find nothing, it may have escaped me, if you can tell me were is this explained or show me an example I would appreciate it

You don’t need to do any calls, Frontity does that for you in the background when you visit a URL.

The data that Frontity fetches matches the data of WordPress for that URL.

The current URL is always accesible using state.router.link.

And you can get the data of any URL using state.source.get().

If you combine both, you can get the URL of the current URL: state.source.get(state.router.link).

If you need to access the data of a another URL you can do so as well. That data is not fetched automatically, but you can fetch it manually with actions.source.fetch.

For example, if you want to fetch the posts of a specific tag, you do actions.source.fetch("/tag/my-tag") and then access the data using cont data = state.source.get("/tag/my-tag").

Be aware of checking first for data.isReady before using that data object as the fetch can take a couple of seconds.

Could you please describe in detail what are you trying to do?

  • Which URLs do you need?
  • What is the data you expect on those URLs?

For example, I am trying to get the post order by the author. So I would use posts/?orderby=author but I don’t know how to get that data. If I try to use actions.source.fetch(’/posts/?orderby=author’) or actions.source.fetch(’?orderby=author’) I get data.is404.

The url that returns the data needed
https://public-api.wordpress.com/wp/v2/sites/site.wordpress.com/posts/?orderby=author

Hi @g.rabello :blush:

It depends on the case. First of all, the method you are trying to use, fetch, works with page URLs, not with the API. What it does is calling a handler and the handler is the one calling the API and populating the state.

For example, if you want to build a page with the last posts ordered by author as you say, the best way would be to create a specific handler for that.

You should add something like this at the index.js of your theme:

// Create the handler
const postsByAuthorHandler = {
  priority: 10,
  pattern: "/posts-by-author/",
  func: async ({ route, state, libraries }) => {
    const { api, populate } = libraries.source;
    // 1. fetch the posts you want
    const response = await api.get({
      endpoint: "posts",
      params: {
        orderby: "author"
      }
    });

    // 2. populate response
    const items = await populate({ response, state });

    // 3. add data to source
    const currentPageData = state.source.data[route];

    Object.assign(currentPageData, {
      isArchive: true,
      isPostByAuthor: true,
      items
    });
  }
};

//Include the new handler in source
const marsTheme = {
  name: "@frontity/mars-theme",
  roots: { ... },
  state: { ... },
  actions: { ... },
  libraries: {
    html2react: { ... },
    source: {
      handlers: [postsByAuthorHandler]
    }
  }
};

export default marsTheme;

Once this is done, if you do actions.source.fetch("/posts-by-author/") you’ll populate the state and you can get the data with state.source.get("/posts-by-author/").

By the way, if you create the handler this way, if you navigate to https://mysite.com/posts-by-author/" the fetch is done automatically and you can get the data. This is useful as you can check if data.isPostsByAuthor and load a different component if you want.

I hope this info is useful, please let us know if this is what you needed. And if not, share any more questions and we’ll be glad to help.

2 Likes

Can I also get only sticky post instead of all posts?