Standalone Access the WordPress database using $wpdb


if you wish to access the database from your code file which is not placed inside one of the standard plugin locations, you will need to include_once() the wp-db.php file as well as the wp-config.php file. Including only the wp-db.php file will not set the database connection information resulting in an error message like “WordPress could not connect to the database”. It is always advisable to put your functionality inside a plugin. However, if you need it in some cases, this workaround is available. For example, this is the code in a file has_great_code.php in the root/installation directory :
[code lang="php"]

I have a sample working script to display the random posts.

you can view the live working example of the code

Click here to display the random posts (It spawns a new page!)

[code lang="php"]
Program Name: promo.php
Program URI:
Description:     This is just a standalone re-direct the page.
This selects a random page from the last 20 posts
and displays to the advertisement promotion.
Author: Jay
Version: 1.5
Author URI:


// selects a random number
$random = (rand()%$No_of_posts_to_show);

if ( $random < 1 )
$random = 1;

if ( $random > $No_of_posts_to_show-1)
$random = $No_of_posts_to_show;

// post_status must be published and post_tpe is post (not page)
$result = $wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts where post_status='publish' and post_type='post' ORDER BY id DESC LIMIT $random,1");

foreach ($result as $topten)
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
// Jump to the link
header("Location: $link");



For example, I have used the following stand alone code to access wordpress database and list the popular posts with in a blog.

[code lang="php"]


Following Google Searches Lead To This Post: access wordpress post database
wordpress external program file query database
wpdb examples
wpdb wordpress comment
wordpress wpdb
wordpress wpdb query
accessing wordpress database
access wordpress database
wordpress database query
display wordpress database
wordpress wpdb external
accessing the wordpress database from custom pages
wordpress $wpdb get id
wordpress get_results last id
wpdb wordpress
Use Wpdb
save contact forms to database wordpress
wordpress db query tutrol
include wp-config.php connecting database
Connecting to an external database from within a WordPress post
$wpdb insert not work
accessing wordpress database external
wordpress externally wpdb
wpdb externally
wordpress wpdb get results post category
accessing wordpress database from outside
wpdb get error
wordpress $wpdb
wpdb last insert id
access wordpress options from external page
$wpdb wordpress
wordpress wpdb get comments
wordpress use only wpdb
wpdb get_permalink
add access to wordpress database
wordpress database query tutorial
using wpdb class external script
wpdb Class+connection to database
wordpress include wpdb
using wpdb
wordpress access database code
use wpdb in page
wp db file location
wordpress $wpdb how to
search form wordpress external database
access database wordpress from external site
include “wp-load” inside class wordpress
wpdb connect to another database
wordpress mysql query for insert new page
search wordpress get_results query
wordpress $wpdb->insert
wordpress standalone php page
access wpdb externally
wordpress wpdb insert
how to call wpdb from php
accessing wpdb from a plugin file
access wordpress database outside wordpress
wordpress database access
include wordpress database in a file
$wpdb standalone script
wordpress accessing database
wordpress database form
connect to wordpress db manually
wpdb class external files
wp wpdb
how to connect with wp db
access database using config.php file
wordpress access wpdb from plugin
wordpress connect to external database
wordpress database outside
use wpdb outside of wordpress
wordpress get data from database in page
using $wpdb outside of plugin
wordpress external connections error
get_permalink using wpdb
change page ID in wordpress database
using wp db connection
wordpress external script
wordpress “wpdb->update”
get_results category
wordpress query select latest posts external site
wordpress plugin database access
$wpdb make simple query
external database to wordpress
wordpress include wpdb in external file
using wpdb standalone
wordpress db get page id
standalone wpdb application
wpdb standalone
wordpress query other database
wp db connection tutorial
wordpress using external php file in root
wordpress “$wpdb->insert”
connect to a wordpress database outside
how to access a wordpress database
using wpdb outside wp
wordpress plugin db access
include $wpdb
loading wpdb in external php
class wpdb what file source
wordpress get_results example
wpdb- post from category
wp “change page id”
$wpdb category
querying wordpress database to get posts
wpdb order by wordpress
db database wpdb
change db wpdb
including $wpdb in custom class
wordpress plugin read userinfo from wpdb
wordpress search database list results
wordpress database standalone
mysql table wordpress wpdb
how to create a new php page with database operations in wordpress
using wpdb->getresults
wordpress access database
$wpdb->get_results wordpress
$wpdb load
wordpress $wpdb load
$wpdb + wordpress
recover wordpress password “connection information”
wordpress search database
accessing database outside of wordpress
wordpress standalone file
wordpress + comment + count + $pageId
wordpress where get_permalink set?
get post category from database wordpress
external connection query wordpress php
wordpress plugin external database
use $wpdb to get post id
wordpress wpdb tutorial
$wpdb tutorial
$wpdb->insert tutorial
wordpress query wpdb
post data from database as posts wordpress
wpdb not work
getting access informaiton to wordpress database
wordpress database results post
external search wordpress database
wordpress display database info
using wpdb latest posts
use $wpdb outside wordpress
wpdb tutorial
how to use wpdb wordpress
wordpress php form to insert and retrieve data from a database
wpdb select
use external database wordpress
standalone wordpress script
wordpress” $wpdb->insert”
wordpress get_results
wordpress how to include database connection
use $wpdb
access wordpress db
wordpress searchable database
wpdb close connection
wordpress database echo title instead of id
how does a wordpress access wpdb
wordpress accessing extrnal database
wordpress “get page ID” from title
form to database wordpress
wp-db.php select categories with posts
wordpress select another database through wpdb
wpdb outside directory
using wpdb in file
wordpress how to include wpdb file
wpdb tutorial insert and update
examples of wordpress with database
wpdb insert wordpress
$wpdb->insert not working
use $wpdb in external file
wordpress load only wpdb
wordpress $wpdb->get_results
wpdb insert on options page
wpdb get results
wordpress loading database class
how to use wpdb
worspress wpdb get data
include $wpdb in files
display wordpress page in external file database
form save to wordpress database
standalone $wpdb
wordpress database titles
how to connect to wordpress database for a plugin
wordpress connecting to external database
working with wordpress database
$wpdb get categories
“$wpdb” wordpress
php code to latest post from wordpress db
php code to display latest post connect to wp db
wordpress page access custom MySQL table display information from that database on my wordpress page.
use wordpress db class to connect to other db
wordpress shopping cart searchable database
wordpress database insert example
wordpress create external database
wordpress external script last posts
wpdb “insert->id”
Not able to update DB with $wpdb
wpdb outside wordpress
wordpress accessing external databes queries
using $wpdb in theme
wpdb examples wordpress
standalone wordpress database connection
$wpdb connection
access database wordpress
wordpress include connection
how can i make an access database stand alone?
access wordpress from php script
use wpdb in external scripts
wordpress limit access to post using paypal
wordpress post from external database
query wordpress database post link
include wpdb wordpress
wordpress wpdb lastid
getting in $wpdb in plugin
wordpress read from external database
include wpdb
wordpress database plugin acces
like query db wordpress wpdb
wpdb category
using wordpress wpdb
wordpress with access database
standalone access to wordpress database
wordpress database
How to close connections to WordPress database
wpdb mysql error
php script connect to wordpress database
+wordpress +wp-load +database
php connect to wordpress database via config.php
accessing wordpress externally
$wpdb->query page id wordpress
$wpdb-&gt in wordpress
wpdb include
wordpress using wpdb
wpdb in wordpress
how to use wpdb externally
wordpress get_results order by
accessing other database from wordpress
script connect db wordpress
external php wordpress get
wordpress loading the core
wordpress database query posts from category
wordpress wpdb last error
wordpress php database insert
get last error from wpdb
wpdb get_results for
wordpress file access db
query DB from wordpress page
wordpress connecting to data base through post
how access database + wordpress
$wpdb include
sample wordpress database
example database forms in wordpress
connect wordpress db
wpdb get_results custom db
random posts wordpress SELECT
wordpress load wpdb only
wordpress database direct access
$wpdb class code
wpdb query include_once
wordpress custom php script conect database
passing wordpress info to an external page
wordpress custom php script connect database
wordpress wpdb example
wordpress use database from external file
wordpress standalone database access
include wpdb class
php connect to wordpress database
where is $wpdb file
$wpdb post categories update
how to use wpdb in page
include wpdb class outside wordpress
wpdb->get_results orderby
$wpdb result
what to include to use $wpdb in all php files
$wpdb wordpress post title
wp how to access db
wpdb->insert last id
wordpress calling external database
how do you use wpdb