0

I understand we can use WP_QUERY and search for posts with the word blog on it and then loop the results, but that probably isn't suitable for production, because it will lead to bugs, so I am wondering what's the recommended way of doing it. I am thinking of using a page template and then loop the post there, but is there a native post type for blog posts?

The issue is there are a lot of pages, a lot of templates, and I am wondering if there's a native page or post type for blog post and how to properly loop using these native types.

https://www.jumboluxurycars.com/blog

It has to look like this.

3
  • 1
    Have you looked at the documentation or the default themes? There’s only one correct way to do it and it’s the way the default themes do it. Commented Dec 9, 2021 at 0:03
  • "Posts" are already that post type, type post, and in a default install of WP every page that lists posts is that page. If you don't want the homepage to be where your latest posts are listed you can use the settings to set a new blogs page. Also beware of using WP_Query to modify which posts WordPress shows, that's what the pre_get_posts filter is for. By using a second replacement query you introduce lots of new problems that need new workarounds, this is why pagination not working is a common problem ( the pagination is for the main query, not the new custom WP_Query )
    – Tom J Nowell
    Commented Dec 9, 2021 at 1:28
  • This sounds like it might be an X Y problem though, where you didn't ask how to do something, rather you devised a solution but didn't know how to implement it and tried to ask about that instead and assumed we already knew all the context. I'm guessing you use Posts for lots of other things and you want a listing of posts but a specific collection of posts, e.g. news items? The answer to the question you asked is that Posts are the native blog post type, but I see you've downvoted the answer below that says this so there must be more you've not told us?
    – Tom J Nowell
    Commented Dec 9, 2021 at 1:31

1 Answer 1

0

The correct way to create a Blog page in WordPress is to create a page and then in Settings > Reading assign that page as the Posts page.

To create or customise the template used by the blog, you need to refer to the Template Hierarchy. You'll notice that the "Blog Posts index page", which is the same as the Posts page you set earlier, will use home.php if it exists, otherwise it will fall back to index.php. So to create a template for your blog page you should either create home.php, or use index.php if the blog should have the same template as search results, category archives, date archives etc.

Inside that template you just need the standard WordPress loop. WordPress has already queried the correct posts, you just need to display them. You absolutely should not be using new WP_Query() to query any posts yourself.

You can cross reference the template hierarchy and the templates inside the default themes to see how they achieve their blogs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.