0

I have a custom database table that contains some data. At the moment, I just want to display an overview of the values contained in the table in WordPress Admin area.

add_filter('manage_mycustom_posts_columns', array($this, 'xxx_columns_head'));
add_action('manage_mycustom_posts_custom_column', array($this, 'xxx_columns_content'));

public function xxx_columns_content($column_name) {
    global $post;
    $post_ID = $post->ID;
    $values = $this->xxxx_get_cpt_data($post_ID);
    //print_r($values);
}

public function xxxx_get_cpt_data($post_ID) {
    $cpt_values = get_post_custom($post_ID);
    if ($cpt_values) {
        return $cpt_values;
    }
}

I realize I can't use the function get_post_custom() and the $post_ID isn't used in my custom table. So how can I make a function xxx_get_post_custom(...) so that I can populate the overview values?

[edit]

    public function xxxx_get_cpt_data() {
    global $wpdb;
    $sql = "SELECT * FROM custom_table";
    $cpt_values = $wpdb->get_results($sql);
    //print_r($cpt_values);
    if ($cpt_values) {
              return $cpt_values->column_name;
         }else {
              return ' ';  
         }

}

And for the columns contents:

public function xxxx_columns_content($column_name) {

        $values = $this->xxxx_get_cpt_data();
        print_r($values);//

      foreach ($values as $value) {
        //print_r($value[0]);
        $value = $value[0];
            if ($column_name == 'name') {
                $name = isset($value['name']) ? esc_attr($value['name'][0]) : '';
                echo $name;
            }
.....other columns content
        }
  }

}

But now I get all database table content for each column, all in one row.

  • You may run any query you desire with wpdb class. Normally custom_post_meta is saved in {$dbPrefix}_postmeta table. You may use filters to reroute functionality on all meta related functions, but since I never needed to, my experience with this ends here. I recommend this read. – Andrei Gheorghiu Nov 28 '15 at 23:46
  • In the majority of cases, custom post types, custom taxonomies, or custom post meta are a better choice. The prime use case of custom database tables is integrating with other software and CMS' – Tom J Nowell Nov 29 '15 at 5:19
2

Yes you have to write a custom function and call it on your manage columns it will help you to get the necessary contents.

     public function xxxx_get_cpt_data($post_ID) {
          //        $cpt_values = get_post_custom($post_ID);
           global $wpdb;

             $sql = "SELECT * FROM ". $wpdb->prefix. "custom_table WHERE post_ID=". $post_ID. " LIMIT 1" ;
              $cpt_values = $wpdb->get_results($sql);
            if ($cpt_values) {
                  return $cpt_values->column_name;
             }else {
                  return ' ';  
             }
       }

But the get_results returns an array of objects. If you wish to get a single result than use get_var. If you have multiple values on the custom table go with get_results use it like this $obj->coulmn_name;

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.