1

I am building a custom application based on WordPress. I have my custom tables in the WP database. Can I use $wpdb to work with them, or is that class solely for the WP related tables?

For example, If I have a database that keeps up with dogs and cats, and I create a custom table named animals (animalid, animal_type, etc.). Do I use $wpdb?

I don't know if "interacting with the WordPress database" with $wpdb is intended solely for the stock tables or not.

EDIT: I created the tables by hand with MySQL Workbench. They were tables upsized from Microsoft Access. I have not "informed" WordPress of their existence or know if I should.

2

Yes you can. Straight from the codex

The $wpdb object is not limited to the default tables created by WordPress; it can be used to read data from any table in the WordPress database (such as custom plugin tables). For example to SELECT some information from a custom table called "mytable", you can do the following.

$myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );
  • But should I be using the custom $wpdb->some_method, like ->update(), insert(), etc. or do I use get_results and use raw SQL? I prefer SQL. – johnny Aug 14 '14 at 19:55
  • 1
    I would use the built in methods where possible because that's why they exist but if you prefer a more raw approach you can use the get_results method. – Welcher Aug 14 '14 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.