Accessing WordPress with CakePHP

If you’re like me, you’ve fallen in love with CakePHP. It’s a tight bit of code that’s easy to use and once you have your application using it, easy to maintain and add new features. For my recent redesign of my freelance site www.systemsevendesigns.com I wanted to be able to display my 3 most recent [...]

If you’re like me, you’ve fallen in love with CakePHP. It’s a tight bit of code that’s easy to use and once you have your application using it, easy to maintain and add new features. For my recent redesign of my freelance site www.systemsevendesigns.com I wanted to be able to display my 3 most recent blog posts on the homepage. Both sites run off of different databases, so how do we do it with Cake? (This post assumes you know a little bit about Cake, but is not too terribly complex. Cake newbies welcome!)

First off we will need to figure out a way to connect to our blog database.

In the app\config\database.php add the following bit of code:


var $blog = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'mymysqlhost',
'login' => 'mymysqlusername',
'password' => 'mysqlpassword',
'database' => 'wordpress database name',
'prefix' => '');

After adding the above your app\config\database.php should look similar to this:


var $default = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'thisusername',
'password' => 'thispassword',
'database' => 'main site db name',
'prefix' => '');

var $blog = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'mymysqlhost',
'login' => 'mymysqlusername',
'password' => 'mysqlpassword',
'database' => 'wordpress database name',
'prefix' => '');

We are setting up another array containing connection data that can be used by models in our application.

Step two is to create a model that will use this connection to give us data.

Create the file: app\models\blog.php and use the following code:

class Blog extends AppModel
{
var $name = 'Blog';
var $useDbConfig = 'blog';
var $useTable = 'wp_posts';
}
?>

The two key lines above are var $useDbConfig = 'blog'; which tells us that we want to use the $blog array found in app\config\database.php and var $useTable = 'wp_posts'; which tells our Blog model to not look for a table called blogs, but one called wp_posts (which is WordPress’ post table)

Now that we have created a blog model we can query this table just like any other database table. Take this bit of code for example:


class SampleController extends AppController{

var $name = 'Sample';
var $uses = array('Blog');

function index(){
$posts = $this->Blog->findAll(null,null,'post_date DESC',3);
$this->set('posts',$posts);
}

}
?>

The above code will set the $posts variable in my view to be equal to the last 3 posts ordered by post_date descending (or newest to oldest)

Quick Notes: This will not return the categories that the blog is posted in or the number of comments, etc… you would have to create a few other models (similar to like what we have done here) and associate them.

That’s all there is too it. Easy as pie cake!