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!
34 Responses
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.
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.
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 http://www.simonsffl.com
Thanks this is just what i needed. I can now start saying goodby to WP

thanks
regards,
Perfect
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.
Is it possible to have WordPress residing in the root, while cakePHP resides in a sub-directory?
If that’s possible, how would the .htaccess’es looked like for the entire directory tree?
Thank you for any solution.
[...] WebDevKungfu Blog Archive Accessing WordPress with CakePHP Posted by root 1 hour 2 minutes ago (https://webdevkungfu.com) Dec 27 2006 in the app config database php add the following bit of code step two is to create a model that will use this connection to give us data i just realised comment 1 is from me as well and ive read this article before webdevkungfu is proudly powe Discuss | Bury | News | WebDevKungfu Blog Archive Accessing WordPress with CakePHP [...]
[...]webdevkungfu.com is another useful authority of information. Online Car insurance claims [...]
This is an excellent write up!
Saved me at least a few hours of figuring it out myself.
Thanks a lot
I love betting on National Football League. The bad part is that I’ve lost about $ seven this month. I think the most enjoyable part is doing the research and using research to find holes in the odds.
very well information you write it very
clean. I’m very lucky to get this information from you.
Football is a bit tiring game but it has given me great excitement and it is a good physical exercise too.~*~
i am not a fan of having credits and getting credits cards..”"
elinize sa?l?k çok güzel yaz?
i had a bad credit score last year but i was able to fix it now.”*,
i am self-studying web design coz building websites is my passion;~-
hey great site
web design is also a good hobby and a great job too. web design jobs pays very well-”
as much as possible i also mantain a good credit record so that i can do business transactions with ease;.”
Hello everybody
If you have an expirience with making graphic to web on-line shop please let me know.
I’m looking for somebady hwo can create and implement graphics to on-line shop.
My sites are:
[url=http://www.mega-power.pl/odzywki-weglowodanowe-c-31.html]weglowodany[/url]
[url=http://prawo-jazdy-mokotow.dogory.pl]prawo jazdy mokotow[/url]
[url=http://prawo-jazdy-warszawa.dogory.pl]prawo jazdy warszawa[/url]
[url=http://www.wlasne-m.waw.pl]mieszkania[/url]
Best Regards
we can’t avoid to have some credit on banks or from other people, there are just times that we are short of money:.-
i love web design and i love to build webpages specially with flash animation”~
we may always need to do some credit specially if we weant to invest on something :”*
both basketball and football are sports that are very close to my heart. i love them both but football is my passion ~*;
You rock
both basketball and football are sports that are very close to my heart. i love them both but football is my passion ~*;
It’s not often that I search for something and actually find useful information… glad I found you… keep writing!
4 Hour Body PAGG
I’ve been trying to create model for wp_terms, wp_term_relationships and wp_term_taxonomy so that I can create post along with category and/or tags. but its does not work. Could you show how did you relate the model relationship for this please? thanks!
BR,
Kerja Part Time
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.
both basketball and football are sports that are very close to my heart. i love them both but football is my passion ~*;
This is just the kind of detail I was trying to find. I wish I’d have found your web site earlier.
Want to integrate with #Twitter? Need an #Application? Custom made #API? http://t.co/jTHAGkqt #website #developer
Wow! You look at this one. Perfect, you will love him.
Your both basketball and football are sports that are very close to my heart. i love them both but football is my passion ~*;