Use $wpdb in wordpress theme files

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?

Related posts

Leave a Reply

2 comments

  1. 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";