0

I have to add an archive widget to a blog based on Gridsome (Wordpress as data source). Standard feature like this one:

enter image description here

I fetched posts with static query:

query Home($page: Int) {
  allWordPressPost(page: $page) {
    pageInfo {
      totalPages
      currentPage
    }
    edges {
      node {
        date(format: "YYYY-MM-DD")
        id
        title
        path

      }
    }
  }
}

Next on monthed hook, i grouped data by year and month to such output:

 {
  "2019": {
    "10": [
      {
        "date": "2019-10-29",
        "node": {
          "date": "2019-10-29",
          "id": "145",
          "title": "Aspernatur ipsam est omnis praesentium rerum autem",
          "path": "/2019/10/29/aspernatur-ipsam-est-omnis-praesentium-rerum-autem/"
        }
      },
      {
        "date": "2019-10-29",
        "node": {
          "date": "2019-10-29",
          "id": "121",
          "title": "Libero voluptatibus facere aspernatur",
          "path": "/2019/10/29/libero-voluptatibus-facere-aspernatur/"
        }
      }

    ],
    "09": [
      {
        "date": "2019-09-25",
        "node": {
          "date": "2019-09-25",
          "id": "1",
          "title": "Hello world!",
          "path": "/2019/09/25/hello-world/"
        }
      }
    ]
  }
}

I generated widget links structure using and standard v-for loop syntax, but sample routes such as: "http://localhost:8080/blog/2018", "http://localhost:8080/blog/2018/19" go to the 404 page. I tried to modify gridsome.config.js by adding:

  templates: {
    WordPressArchive: [
      {
        path: "/blog/:year",
        component: "./src/templates/WordPressArchive.vue"
      }
    ]

  },

It results in error:

 "Error: A content type for the WordPressArchive template does not exist"

How can i enable archive routes in Gridsome to filter blog posts by year and year/month ?

"/blog/:year",
"/blog/:year/:month"

Is there a possibility to group posts by date and year directly from graphQL query, without further js manipulations?

user1276919
  • 550
  • 5
  • 25

1 Answers1

1

You have a WordPressPost type in your GraphQL and you are trying to use WordPressArchive as the template name. These need to match, either change the typeName in GraphQL or change the template name in gridsome.config.js.

Evgeny
  • 6,533
  • 5
  • 58
  • 64