Saturday, October 26, 2013

Execute Database Query In some Custom Files In wordpress

Hiiiiiii, This post is for all those who are developing custom Wordpress plugin and for those who want to write some custom codes to the wordpress folders and they want to execute some database query  in the same manner as wordpress does.
The steps shown in this post are very easy and by using these steps you can very frequently execute any of your query in wordpress.




Steps To Execute Query In Wordpress  :

1. Include the "wp-load.php" file in your code. "wp-load.php" is placed in the main wordpress directory in wp-admin,wp-content is placed. If I am developing a custom file in wp-admin directory then I'll write "require_once('../wp-load.php');" to include this "wp-load.php" file.

2. Write "global $wpdb;" to use wordpres global object "$wpdb" and functions performed by it.

3. Then write your query as shown below :

$sql="update ".$wpdb->prefix."posts SET 
post_title        = '".$title."',
post_content        = '".$contecnt."'
WHERE post_id  = '".$post_id."'";

Here $wpdb->prefix  returns the prefix you have used for your database tables at the time of installation. I have used wp_ as a prefix for my tables so by writing $wpdb->prefix."posts" , I'll get my table with tablename as "wp_posts".


4. Now write  $wpdb->query( $sql); to execute your query.




Hope you like this post……..Please Comment…………!!!!!!!!!


Tags: ,

0 Responses to “Execute Database Query In some Custom Files In wordpress”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks