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.


1 Answer 1


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
    Commented Aug 14, 2014 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
    Commented Aug 14, 2014 at 20:21
  • I register tables by adding them to the $wpdb object as it makes them easily available: $wpdb->my_custom_table = "{$wpdb->prefix}my_custom_table" and then I only ever use $wpdb->my_custom_table which allows the table name to be changed to anything the site owner likes.
    – Ryan Bayne
    Commented Sep 29, 2019 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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