Snippets

Jenny Rasmussen Pulling Most Recent WordPress Post

Created by Jenny Rasmussen last modified
namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class YourController extends Controller
{
    public function index()
    {
        $post = DB::connection('db_connection_name')
            ->table('posts')
            ->join('users', 'posts.post_author', '=', 'users.ID')
            ->select(array('posts.post_title', 'posts.post_content', 'users.display_name', 'posts.post_name'))
            ->where('post_status', '=', 'publish')
            ->orderby('post_date', 'desc')
            ->first();

        $post_content = wpautop($post->post_content);
    
        return view('home', compact('post', 'post_content'));
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.