Accessing WordPress with CakePHP

Posted by justinjohnson on Dec 27th, 2006

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:

PHP:
  1. var $blog = array('driver' => 'mysql',
  2. 'connect' => 'mysql_connect',
  3. 'host' => 'mymysqlhost',
  4. 'login' => 'mymysqlusername',
  5. 'password' => 'mysqlpassword',
  6. 'database' => 'wordpress database name',
  7. 'prefix' => '');

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

PHP:
  1. var $default = array('driver' => 'mysql',
  2. 'connect' => 'mysql_connect',
  3. 'host' => 'localhost',
  4. 'login' => 'thisusername',
  5. 'password' => 'thispassword',
  6. 'database' => 'main site db name',
  7. 'prefix' => '');
  8.  
  9. var $blog = array('driver' => 'mysql',
  10. 'connect' => 'mysql_connect',
  11. 'host' => 'mymysqlhost',
  12. 'login' => 'mymysqlusername',
  13. 'password' => 'mysqlpassword',
  14. 'database' => 'wordpress database name',
  15. '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:

PHP:
  1. class Blog extends AppModel
  2. {
  3. var $name = 'Blog';
  4. var $useDbConfig = 'blog';
  5. var $useTable = 'wp_posts';
  6. }
  7. ?>

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:

PHP:
  1. class SampleController extends AppController{
  2.  
  3. var $name = 'Sample';
  4. var $uses = array('Blog');
  5.  
  6. function index(){
  7. $posts = $this->Blog->findAll(null,null,'post_date DESC',3);
  8. $this->set('posts',$posts);
  9. }
  10.  
  11. }
  12. ?>

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!

5 Responses

  1. Iain Says:

    Perfect - just what i was looking for - thanks! I’ve got a site currently with wordpress integrated and am now considering migrating the site to Cake.

  2. Carey Baird Says:

    Great tutorial, thanks!

    In order to get only posts, I added a couple of conditions the the findAll call in the controller, as follows:

    $posts = $this->Blog->findAll(array(’post_type’=>’post’, ‘post_status’=>’publish’),null,’post_date DESC’,3);

    Hope it helps someone - I am working on an elegant way to generate a link (using a custom post slug), I will add when I have figured that out.

  3. Iain Mullan Says:

    Thanks! I’d had a go at this already, hadn’t thought of defining a separate DB connection for wordpress - makes sense though.

    One suggestion. If you’re defining a separate connection, then it might be handy to make use of the prefix field, set it to ‘wp_’ , then you can define a model called Post , and there’s no need to redefine var $useTable in the model.

    Would save a bit of time if you want to start using other WP tables as well as wp_posts.

    Cheers!
    Iain

    PS. I just realised comment #1 is from me as well, and ive read this article before :) ….seems like a loooong time ago now. The newly migrated Cake site is www.simonsffl.com

  4. Perfect Says:

    Thanks this is just what i needed. I can now start saying goodby to WP
    :)

    thanks
    regards,
    Perfect

  5. Vjhere Says:

    Hi!
    Its really great that i find this wordpress integration with cakephp. but i have stuck at where to place my blog files also how to manage cakephp users table with wp_users table so my application users will also become a blog users.

    Thanks in Advance.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Advertisements

Recent Posts

Categories

Archives

Meta: