Skip to content

Instantly share code, notes, and snippets.

@web-hat
Created March 27, 2017 18:17
Show Gist options
  • Save web-hat/f35ce75e59cf61823795a41bcd7e698f to your computer and use it in GitHub Desktop.
Save web-hat/f35ce75e59cf61823795a41bcd7e698f to your computer and use it in GitHub Desktop.
Accessing WordPress Post through Laravel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BlogPost extends Model {
protected $connection = 'wordpress_db_connection';
protected $table = 'posts';
protected $primaryKey = 'ID';
/**
* Get the postmeta for the blog post.
*/
public function postmetas() {
return $this->hasMany('App\BlogPostmeta', 'post_id');
}
/**
* Get comments from the blog post.
*/
public function comments() {
return $this->hasMany('BlogComment', 'comment_post_ID');
}
/**
* Filter by post type
*/
public function scopeType($query, $type = 'post') {
return $query->where('post_type', '=', $type);
}
/**
* Filter by post status
*/
public function scopeStatus($query, $status = 'publish') {
return $query->where('post_status', '=', $status);
}
/**
* Filter by post author
*/
public function scopeAuthor($query, $author = null) {
if (!empty($author)) {
return $query->where('post_author', '=', $author);
}
}
//Method to get post with postmeta
Public function getPosts() {
return BlogPost::with('postmetas')
->status()
->type()
->get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment