Standalone Access the Wordpress database using $wpdb
- Disable and Turn Off Post Revisions Tracking in WordPress 2.6 or Above
- How to access Oracle Applications (Core Application & Forms) from Linux Client ?
- Simple Paypal Shopping Cart Settings – wordpress integration – Using Paypal Payment Method.
- Good security measures to keep WordPress safe – How to Secure Wordpress?
- Google Adsense Wordpress Plugins
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-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!)
/*
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 < 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;
$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.
<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–>

you save me .
cool
You rock. World needs more souls like you.
Super great. Now to build a function for posting to wordpress from an external database!
thanks a lot…
it really helped to finish a small part in my project…
thank.. it really helped..
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.
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.
i can’t get it to work.. maybe it will not work with wp2.9
Thanks Jiltin for this article, I am using WP 2.9.1 and it’s working like a charm
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
Wordpress