Standalone Access the Wordpress database using $wpdb

Posted by Jiltin     15 October, 2008    16,960 views   

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 :

include_once(‘wp-config.php’);
include_once(‘wp-load.php’);
include_once(‘wp-includes/wp-db.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!)

<?php
/*
Program Name: promo.php
Program URI: http://www.notesbit.com/
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: http://www.notesbit.com/
*/

include_once(‘wp-config.php’);
include_once(‘wp-load.php’);
include_once(‘wp-includes/wp-db.php’);

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

if ( $random &lt; 1 )
{
$random = 1;
}

if ( $random &gt; $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-&gt;get_results("SELECT ID,post_title FROM $wpdb-&gt;posts where post_status=’publish’ and post_type=’post’ ORDER BY id DESC LIMIT $random,1");

foreach ($result as $topten)
{
$postid = $topten-&gt;ID;
$title = $topten-&gt;post_title;
$commentcount = $topten-&gt;comment_count;
$link=get_permalink($postid);
}
// 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.

<div class="box" id="featured">
        <h2>Popular Post</h2>
        <ul>
        <?php
        $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts where post_status=’publish’ ORDER BY comment_count DESC LIMIT 0,15");
        foreach ($result as $topten) {
        $postid = $topten->ID;
        $title = $topten->post_title;
        $commentcount = $topten->comment_count;
        if ($commentcount != 0) {
        ?>
        <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li>
        <?php } } ?>
        </ul>
</div>  <!–/Popular featured box–>

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
wpdb->insert
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
themes+wordpress+accessibility
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
$wpdb->insert
how to use wpdb wordpress
$wpdb->posts
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+wpdb+login
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
wpdb->update
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.
$wpdb->update
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

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : Featured, Web & Scripts Tags : , , ,

Comments
March 5, 2009

you save me .

cool

Posted by domolo
May 20, 2009

You rock. World needs more souls like you.

Posted by BuY bEdDiNg
November 23, 2009

Super great. Now to build a function for posting to wordpress from an external database!

Posted by Abbotsford
December 15, 2009

thanks a lot…
it really helped to finish a small part in my project…

Posted by ajith
January 7, 2010

thank.. it really helped.. :)

Posted by Ralph
January 22, 2010

The most quickly way to check out the quality of the thesis writing service is to order the the best history dissertation just about this good topic from the dissertations writing services.

Posted by RUBYqZ
January 28, 2010

That would take a long time to master a writing technique. But in some cases, some people are lack of time. If you are willing to save your time and have the best quality online essay, you will opt for the progressive research papers writing service and buy custom essay right there. After that, your academic success is received.

Posted by EllenyW35
January 29, 2010

i can’t get it to work.. maybe it will not work with wp2.9

Posted by EL
January 30, 2010

Thanks Jiltin for this article, I am using WP 2.9.1 and it’s working like a charm

Posted by moonpixel
February 22, 2010

The wpdb class (WordPress DataBase class) is based on the ezSQL class, and handles database connections and queries.

Why use it? You can make all sorts of custom queries, and pull information from the wordpress database, including posts! Read on for more information on this useful class

Posted by window shades
February 22, 2010
Posted by window shades
Leave a comment

(required)

(required)