I am using the Custom List Table Example plugin as a base to display entries from a table I created in the wordpress database...

However I am having issues with this function

function column_default($item, $column_name){


I get the error message:

Fatal error: Cannot use object of type stdClass as array in

In the example in the plugin, it uses a simple array. But the $data returned from the query returns several rows of data (i.e. an array with object, object, object).

Inside of my prepare_items() function:

 global $wpdb;
          $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'name'; //If no sort, default to title
          $sql = "SELECT * FROM wp_nc_location ORDER BY " . $orderby;
          $data = $wpdb->get_results($sql);
  • I think the problem is in the IF statement. Try just using $sql = "SELECT * FROM wp_nc_location ORDER BY title.
    – Steven
    Aug 15, 2011 at 21:28
  • Nope. Still the same error Aug 15, 2011 at 21:30
  • 2
    Please post the whole function and add echo '<pre>'; var_dump($data); echo '</pre>'; add the end to it and post the result.
    – kaiser
    Aug 15, 2011 at 22:09
  • You may wish to take a look at the example plugin to get a better grasp of how to use the class.
    – t31os
    Aug 16, 2011 at 11:59

3 Answers 3

$data = $wpdb->get_results($sql, ARRAY_A);

Adding ARRAY_A forces get_results to return an associative array.


This is what I use to retrieve data from custom tables:

  // Retrieve all seasons registered
  function get_registered_seasons() {
    global $wpdb;

    $table_name   = $wpdb->prefix . "cwgallery";    
    $sql          = "SELECT DISTINCT year, season FROM $table_name ORDER BY year DESC ";
    $result  = $wpdb->get_results($sql);

    return $result;
  • When I print_f on my $data it's showing my data (in array form)...so that part is working. My problem is that I am not very familiar with the built-in Wordpress Class to List a table in the admin section... Aug 15, 2011 at 21:39

Got it working by passing $item as an array

function column_default($item, $column_name){
$item = (array)($item)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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