Headless Wordpress/NextJS app not returning blog posts
Answered
Cape lion posted this in #help-forum
Cape lionOP
I have two files
My
Then I built a
I am not getting any errors in production or development but when I put the
I am not sure why the
src/app/lib/api.js which has the following:const API_URL = process.env.WORDPRESS_API_URL;
async function fetchAPI(query, { variables } = {}) {
const headers = { 'Content-Type': 'application/json' };
const res = await fetch(API_URL, {
method: 'POST',
headers,
body: JSON.stringify({ query, variables }),
});
const json = await res.json();
if (json.errors) {
console.log(json.errors);
console.log('error details', query, variables);
throw new Error('Failed to fetch API');
}
return json.data;
}
export async function getAllPosts(preview) {
const data = await fetchAPI(
`
query AllPosts {
posts(first: 20, where: {orderby: {field: DATE, order: DESC}}) {
edges {
node {
date
id
slug
title
content
}
}
}
}
`
);
return data?.posts;
}My
API_URL is defined in .env and in my environmental variables from the Graphql Wordpress plugin...Then I built a
component to house the api call and to display the blog posts app/src/(components)/Wordpress.js 'use client';
import { getAllPosts } from '../lib/api';
const Wordpress = ({ allPosts }) => {
// Check if allPosts and edges are defined before accessing their properties
const edges = allPosts ? allPosts.edges : [];
return (
<section>
{edges.map(({ node }) => (
<div key={node.id}>
<p className='text-3xl font-cursive text-black'>
{node.title}
</p>
</div>
))}
</section>
);
};
export default Wordpress;
export async function getStaticProps() {
const allPosts = await getAllPosts();
return {
props: {
allPosts,
},
};
}I am not getting any errors in production or development but when I put the
Wordpress component into a test page it just creates an empty div I am not sure why the
node.title is not populating. The query is returning results at the Wordpress GraphiQL IDE.Answered by Cape lion
SOLVED: I got some help working out a few de-structuring issues. I was also not passing through the right parameters to the components and got it working! Here is the link to the commit to see what was changed if you are interested
https://github.com/Isaac-Tait/wordpress_nextjs/commit/fdbf7142ecb5138565b7d34181f1dabc3e6951a8
https://github.com/Isaac-Tait/wordpress_nextjs/commit/fdbf7142ecb5138565b7d34181f1dabc3e6951a8
11 Replies
Cape lionOP
I also tried this in the
(component)/Wordpress.jsimport { getAllPostsForHome } from '../lib/api';
export default function Wordpress({ allPosts, preview }) {
const post = allPosts && allPosts.edges[0]?.node;
return (
<div preview={preview}>
{post && (
<div>
<div title={post.title}>
{/* Other properties */}
</div>
</div>
)}
</div>
);
}look like you are using app router
which doesn't have getStaticProps
call the getAllPosts function directly in your page component and pass the data to Wordpress component
Blanc de Hotot
I’m a next.js developer and have many years of experience and have connected to wpgraphql many times
Cape lionOP
I am using
app router but I spun up another next site using page router and it is still not working... https://github.com/Isaac-Tait/wordpress_nextjs/blob/main/src/pages/test.jsthe getStaticProps only execute on pages
so move it to index.js then pass the data the <Blog />
export default function Home({ allPosts }) {}<Blog allPosts={allPost} />Cape lionOP
SOLVED: I got some help working out a few de-structuring issues. I was also not passing through the right parameters to the components and got it working! Here is the link to the commit to see what was changed if you are interested
https://github.com/Isaac-Tait/wordpress_nextjs/commit/fdbf7142ecb5138565b7d34181f1dabc3e6951a8
https://github.com/Isaac-Tait/wordpress_nextjs/commit/fdbf7142ecb5138565b7d34181f1dabc3e6951a8
Answer