How to initialize $wpdb?

First of all you probably created an isolated file that is not under the umbrella of WP files. That’s why you are not getting $wpdb. I guess you may not following the general rules/conventions of theme development. my question is now how are you accessing the file?

whatever, if you include the wp_config.php in your file, you will get the $wpdb in your file.

considering in a directory under themes directory, here is how you include/require the file

require_once ('../../../wp-config.php');

you may need to alter the path based on your system.

Leave a Comment