Accessing WordPress Post through Laravel

Accessing WordPress Post through Laravel


In the previous tutorial we have learn as how to Install WordPress inside a Laravel application in a subdirectory and same domain and access it using In this tutorial we will be learning as how to access WordPress post, page, or any custom post type data through Laravel.
There are 2 ways to do this

  1. Through WordPress REST API
  2. Directly access WordPress database through Laravel Modal

Directly access WordPress database through Laravel Modal

We need to create Modal BlogPost and BlogPostmeta to access data from wp_posts and wp_postmeta table respectively.

Modal BlogPost.php

Modal BlogPostmeta.php

Now as we have set the database connection and model last thing that we need is to retrieve post. In controller we have to use our Post model to get post as use App\BlogPost;.

$posts will hold all your active published post and under postmetas key it will be holding all the meta’s that are related to that specific post.

In next tutorial we will be digging deep into WP Comments and WP Author.

Share this Post:

About Raunak Gupta

I'm an expert Laravel, WooCommerce, WordPress theme and plugin developer, over the time I have developed several sites and applications. I like to help the upcoming developer, So I share my experience through this blog.  

Related Posts:

4 thoughts on “Accessing WordPress Post through Laravel


    What should we add in place of ‘wordpress_db_connection’


    Do you know how to get the blog featured image with this code?

    • Hi Divya,

      Yes you can get the feature image with something like this:

      Assuming in your Modal you have this

      public function getPostbyId($postId) {
      return BlogPost::with('postmetas')
      ->where('ID', $postId)

      now in your controller you can write this code:

      $thumbnailURL = null;

      $objBlogPost = new BlogPost();
      $post = $objBlogPost->getPostbyId($postId); //get post details

      if (!empty($post->postmetas->_thumbnail_id)) {
      $thumbnail = $objBlogPost->getPostbyId($postId); //get feature image details
      $thumbnailURL = !empty($thumbnail->postmetas->_wp_attached_file) ? $thumbnail->postmetas->_wp_attached_file : null; //it will output something ike this 2019/10/banner-image-copy.jpg

      dd('' . $thumbnailURL); // you need to concatenate your wordpress blog site URL


Leave a Reply to Raunak Gupta Cancel reply

Your email address will not be published. Required fields are marked *