Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to use $wpdb in a theme file, but it doesn't work. My code is bellow:

<?php


$action = $_POST['action'];
$updateRecordsArray = $_POST['recordsArray'];


if ($action == "updatelist") {

    $listingCounter = 1;
    $column = $_GET['column'];

    foreach ($updateRecordsArray as $key=>$value) {

    $wpdb->update('wp_postmeta', array('meta_value'=>$column), array('post_id'=>$value, 'meta_key'=>'dbt_text') );
    $wpdb->update('wp_posts', array('menu_order'=>$listingCounter), array('ID'=>$value) );

    $listingCounter = $listingCounter + 1;  

    }
}


?>

I think that $wpdb must be declared global or I must include certain files. Can someone please help me on this?

share|improve this question

migrated from stackoverflow.com Nov 10 '11 at 20:19

This question came from our site for professional and enthusiast programmers.

    
What doesn't work? What errors do you get? –  Pekka 웃 Aug 31 '11 at 7:05
    
Undefined variable: wpdb –  zuzuleinen Aug 31 '11 at 7:07
    
It's probably a scope issue. Is this being included from within a function? Are you initializing Wordpress? –  Pekka 웃 Aug 31 '11 at 7:08
    
I don't think so. The code bellow is all that i have in the php file. –  zuzuleinen Aug 31 '11 at 7:22

2 Answers 2

up vote 2 down vote accepted

You need to connect your script to Wordpress before you can use $wpdb.

The usual way to do this is to include wp-blog-header.php:

 include "/path/to/wordpress/wp-blog-header.php";
share|improve this answer
    
Thanks a lot. But is there an alternative to /path/to/wordpress/ ? Now my path to wordpress is C://xampp/htdocs/new/ and I don't want to use it that way. –  zuzuleinen Aug 31 '11 at 7:32
    
@zuzuleinen if you're in the same directory with this script, you don't need to use the path. wp-blog-header.php alone will do then –  Pekka 웃 Aug 31 '11 at 7:33
    
I'm not in the same directory, I am in my theme directory. Should I copy wp-blog-header.php in my theme files or there's a more elegant way to do this? –  zuzuleinen Aug 31 '11 at 7:35
1  
@zuzuleinen you can use relative paths, e.g. ../../../wp-blog-header.php Do not copy the header file, it won't work –  Pekka 웃 Aug 31 '11 at 7:36
2  
if you want it to work in all cases (you could suddenly decide you want to have subdirectories in your theme etc...), you can also get the wp-header-file like this: get_option('home') . "/wp-blog-header.php"; this will always work. –  Michiel Standaert Aug 31 '11 at 8:14

Before you can use wpdb class:

global $wpdb;
share|improve this answer
    
exactly, you need to call the global wpdb varaible by using global $wpdb; –  moffepoffe Aug 31 '11 at 11:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.