Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is It possible to query post within the same category in frontmatter? #10801

Closed
sparklesam opened this issue Jan 3, 2019 · 4 comments · May be fixed by saurabharch/gatsby#2383 or ajesse11x/gatsby#2220
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@sparklesam
Copy link

Summary

I'm trying to show related post under the same frontmatter categories. Is it possible to do this within a GraphQL query?

In page template for blog post, I have already setup another query for frontmatter categories, just dunno how to connect two queries together.

  query BlogPostBySlug($slug: String!) {
    post: markdownRemark(fields: { slug: { eq: $slug } }) {
      html
      timeToRead
      excerpt
      frontmatter {
        title
        cover
        date
        category
        tags
      }
      fields {
        nextTitle
        nextSlug
        prevTitle
        prevSlug
        slug
        date
      }
    }
    related: allMarkdownRemark(
      filter: {
        frontmatter: {
          category: {
             eq: { ???} 
          }
        }
      }
    ) {
       edges {
          node {
             id
            frontmatter {
              title
            }
            }
          }
        }
  }

File contents (if changed)

  System:
    OS: macOS 10.14.2
    CPU: x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.8.0 - /usr/local/bin/node
    npm: 6.2.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 71.0.3578.98
    Firefox: 65.0
    Safari: 12.0.2
  npmPackages:
    gatsby: 2.0.40 => 2.0.40
    gatsby-image: 2.0.19 => 2.0.19
    gatsby-plugin-catch-links: 2.0.6 => 2.0.6
    gatsby-plugin-feed: 2.0.9 => 2.0.9
    gatsby-plugin-google-analytics: 2.0.7 => 2.0.7
    gatsby-plugin-lodash: 3.0.2 => 3.0.2
    gatsby-plugin-manifest: 2.0.7 => 2.0.7
    gatsby-plugin-nprogress: 2.0.6 => 2.0.6
    gatsby-plugin-offline: 2.0.12 => 2.0.12
    gatsby-plugin-react-helmet: 3.0.1 => 3.0.1
    gatsby-plugin-sharp: 2.0.12 => 2.0.12
    gatsby-plugin-sitemap: 2.0.2 => 2.0.2
    gatsby-plugin-styled-components: ^3.0.4 => 3.0.4
    gatsby-plugin-twitter: 2.0.7 => 2.0.7
    gatsby-plugin-web-font-loader: ^1.0.4 => 1.0.4
    gatsby-remark-autolink-headers: 2.0.10 => 2.0.10
    gatsby-remark-copy-linked-files: 2.0.6 => 2.0.6
    gatsby-remark-images: 2.0.6 => 2.0.6
    gatsby-remark-prismjs: 3.0.3 => 3.0.3
    gatsby-remark-responsive-iframe: 2.0.6 => 2.0.6
    gatsby-source-filesystem: 2.0.7 => 2.0.7
    gatsby-transformer-remark: 2.1.11 => 2.1.11
    gatsby-transformer-sharp: 2.1.8 => 2.1.8
  npmGlobalPackages:
    gatsby-cli: 1.1.58

gatsby-config.js: N/A
package.json: N/A
gatsby-node.js: N/A
gatsby-browser.js: N/A
gatsby-ssr.js: N/A

@pieh
Copy link
Contributor

pieh commented Jan 3, 2019

You can pass category alongside slug in gatsby-node when you use createPages, then you could use adjust query to use it:
query BlogPostBySlug($slug: String!, $category: String!) { and eq: $category

@sparklesam
Copy link
Author

Thanks a lot! I have added category: edge.node.frontmatter.category, in the context for createPage. But I was also wondering, how could i add an except condition to avoid duplicate on current article?

query BlogPostBySlug($slug: String!, $category: String!) {
  post: markdownRemark(fields: {slug: {eq: $slug}}) {
    html
    timeToRead
    excerpt
    frontmatter {
      title
      cover
      date
      category
      tags
    }
    fields {
      nextTitle
      nextSlug
      prevTitle
      prevSlug
      slug
      date
    }
  }
  related: allMarkdownRemark(sort: {fields: [fields___date], order: DESC},limit: 3, filter: {frontmatter: {category: {eq: $category}}}) {
    edges {
      node {
        frontmatter {
          title
        }
        fields {
          slug
        }
      }
    }
  }
}

@sidharthachatterjee sidharthachatterjee added the type: question or discussion Issue discussing or asking a question about Gatsby label Jan 3, 2019
@sidharthachatterjee
Copy link
Contributor

@sparklesam You could add another filter that does a not equal check with the current slug

So then the filter section becomes

filter: {
  frontmatter: {
    category: {
      eq: $category
    }
  },
  fields: {
    slug: {
      ne: $slug
    }
  }
}

@sparklesam
Copy link
Author

Thanks a lot for all your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
3 participants