0

Im building a plugin and i made custom wp_list_table class to suit my needs and display items with native wordpress look.

The problem is that i can't get to list categories with categories names.

I use data from database table and list of my categories in database is in comma separated list of categories id's

so the data looks like

136, 8, 10, 11, 110

And when i output that on website i get list just like that comma separated

categories list on my plugin

But i want to display each category name instead of simple id like this so at the end i want to make it display like on wordpress post default list like this

enter image description here

It doesn't need to be clickable i just want to display category names on my custom class.

Any idea how to achieve this?

EDIT:

Here is the whole class that i currently use

class custom_feeds_list_table extends WP_List_Table {
// declare constructor and give some basic params
function __construct() {
    global $status, $page;
    $message = null;
    $type = null;

    parent::__construct(array(
        'singular' => 'custom-feed',
        'plural' => 'custom-feeds',
    ));
}

//List all columns that we want to display in table
function get_columns() {
    $columns = array(
        'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
        'feedname' => __('Feed Name', 'vt-translate'),
        'provider' => __('Selected provider', 'vt-translate'),
        'searchterm' => __('Searchterms', 'vt-translate'),
        'numvideos' => __('Videos to pull', 'vt-translate'),
        'post_category' => __('Categories', 'vt-translate'),
        'feedstatus' => __('Status', 'vt-translate'),
    );
    return $columns;
}

//Select which columns are sortable
function get_sortable_columns() {
    $sortable_columns = array(
        'feedname' => array('feedname', true),
        'provider' => array('provider', false),
        'searchterm' => array('searchterm',false),
        'numvideos' => array('numvideos',false),
        'feedstatus' => array('feedstatus', false),
    );
    return $sortable_columns;
}

// Default column render is required
function column_default($item, $column_name) {
    return $item[$column_name];
}

//Bulk action for delete 
function get_bulk_actions() {
    $actions = array(
        'update' => 'Update',
        'enable' => 'Enable',
        'disable' => 'Disable',
        'delete' => 'Delete'
    );
    return $actions;
}

//Process bulk action in database
function process_bulk_action() {
    global $wpdb;
    $custom_table = $wpdb->prefix . 'custom_feeds';

    if ('enable' === $this->current_action()) {
        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
        if (is_array($ids)) $ids = implode(',', $ids);

        $this->type = 'updated';
        $this->message = __('No feed selected to enable.', 'custom-feeds');

        if (!empty($ids)) {
            $wpdb->query("UPDATE $custom_table SET feedstatus = 1 WHERE id IN($ids)");
            $this->type = 'updated';
            $this->message = __('Selected feeds are enabled, your website will be updated with enabled feeds at scheduled time.', 'custom-feeds');
        }
    }
    if ('disable' === $this->current_action()) {
        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
        if (is_array($ids)) $ids = implode(',', $ids);

        $this->type = 'updated';
        $this->message = __('No feed selected to disable.', 'custom-feeds');

        if (!empty($ids)) {
            $wpdb->query("UPDATE $custom_table SET feedstatus = 0 WHERE id IN($ids)");
            $this->type = 'updated';
            $this->message = __('Selected feeds are disabled, your website will no longer get updates of disabled feeds.', 'custom-feeds');
        }
    }
    if ('delete' === $this->current_action()) {
        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
        if (is_array($ids)) $ids = implode(',', $ids);

        $this->type = 'updated';
        $this->message = __('No feed selected to delete.', 'custom-feeds');

        if (!empty($ids)) {
            $wpdb->query("DELETE FROM $custom_table WHERE id IN($ids)");
            $this->type = 'updated';
            $this->message = __('Selected feeds are deleted, your website will no longer get updates from deleted feeds. ', 'custom-feeds');
        }
    }
}

//Prepare items to show in list
function prepare_items() {
    global $wpdb;
    $evg_table = $wpdb->prefix . 'custom_feeds';

    $per_page = $this->get_items_per_page('feeds_per_page', 20);
    $current_page = $this->get_pagenum();

    $columns = $this->get_columns();
    $hidden = array();
    $sortable = $this->get_sortable_columns();

    // here we configure table headers, defined in our methods
    $this->_column_headers = $this->get_column_info();

    // process bulk action if any
    $this->process_bulk_action();

    // will be used in pagination settings
    $total_items = $wpdb->get_var("SELECT COUNT(id) FROM $custom_table");

    // prepare query params, as usual current page, order by and order direction
    $paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged']) - 1) : 0;
    $orderby = (isset($_REQUEST['orderby']) && in_array($_REQUEST['orderby'], array_keys($this->get_sortable_columns()))) ? $_REQUEST['orderby'] : 'id';
    $order = (isset($_REQUEST['order']) && in_array($_REQUEST['order'], array('asc', 'desc'))) ? $_REQUEST['order'] : 'desc';

    //Define item aray
    $this->items = $wpdb->get_results($wpdb->prepare("SELECT * FROM $custom_table ORDER BY $orderby $order LIMIT %d OFFSET %d", $per_page, $paged), ARRAY_A);

    // configure pagination
    $this->set_pagination_args(array(
        'total_items' => $total_items,
        'per_page' => $per_page,
        'total_pages' => ceil($total_items / $per_page)
    ));
}

//Input checkbox for id
function column_cb($item) {
    return sprintf(
        '<input type="checkbox" name="id[]" value="%s" />',
        $item['id']
    );
}

//Display feed name column with buttons
function column_feedname($item) {
    $actions = array(
        'edit' => sprintf('<a href="?page=custom-feed&action=edit&id=%s">%s</a>', $item['id'], __('Edit', 'custom-feeds')),
        'delete' => sprintf('<a href="?page=%s&action=delete&id=%s">%s</a>', $_REQUEST['page'], $item['id'], __('Delete', 'custom-feeds')),
    );

    return sprintf('%s %s',
        '<a href="?page=custom-feed&action=edit&id=' . $item['id'] . '">' . $item['feedname'] . '</a>',
        $this->row_actions($actions)
    );
}

//Display feed status column
function column_feedstatus($item) {
    return sprintf(
        ($item['feedstatus'] == true) ? '<div style="color:#006505">Active</div>' : '<div style="color:#AA0000">Inactive</div>'
    );
}

//Display category column 
function column_post_category($item) {
    return sprintf(
        $item['post_category']
    );
}

}

As you can see the category column function is at the end and i only use basic code

        return sprintf(
        $item['post_category']
    );

which will return raw data from the database, and that is only ID's of categories with comma separated numbers

136, 8, 10, 11, 110

I know if i use this in theme somewhere like this

$cats = $item['post_category'];

it will return

$cats = 136, 8, 10, 11, 110

than

$categories = get_categories( array('include' => $cats) );

and in the end

foreach($categories as $single_cat){
   echo $single_cat->cat_name;
}

it will echo the name of every category.

But that's not the case here, this doesn't work in wp_list_table class for some reason so im looking for some other solution. I also looked at wordpress wp_list_table class in included php file but couldn't find anything related to category.

4
  • Post the code for your class, please
    – s_ha_dum
    Mar 1, 2014 at 20:53
  • I updated the post to show you my class, and i have been searching for little more about the issue. So far i think my best solution is to use wp_list_categories to list all cats, and customize it with custom walker just to find out more on how to customize walker.
    – lonerunner
    Mar 2, 2014 at 10:42
  • Where is this code? Where are you using it?
    – s_ha_dum
    Mar 2, 2014 at 13:46
  • Im using it on my plugin on custom options page to list all the inputs i have previously put in the database.
    – lonerunner
    Mar 2, 2014 at 14:23

1 Answer 1

1

It is very hard to test/debug this kind of question since it is dependent upon a lot of code but it looks to me like you just need to break apart your comma separated list, get the category data, and print the names. get_categories() and wp_list_pluck() should make that pretty easy.

//Display category column 
function column_post_category($item) {
    $c = get_categories(array('include' => $item['post_category']));
    $c = wp_list_pluck($c,'name');
    $c = implode(', ',$c);
    return $c;
}
5
  • Yes yes this did the trick from all the tests that i did i always got back the array and tried to isolate only names from array but no luck. looks like wp_list_pluck does that job i didn't even know that this exists. Thanks for your help.
    – lonerunner
    Mar 2, 2014 at 15:14
  • It isn't hard to get the names with a foreach loop. wp_list_pluck() just makes it easy.
    – s_ha_dum
    Mar 2, 2014 at 15:24
  • Well you see the code i posted in my question with foreach loop i used it in themes many times, there was theme recently i built where i had to pull specific categories by their id's and worked perfectly to display name and description. But for some reason it doesn't work in walker at least not from what i was trying. Anyway i have a problem when there are 2 or more children categories it doesn't display them in the list now.
    – lonerunner
    Mar 2, 2014 at 15:46
  • You mean "categories that are children of the IDs in your list"?
    – s_ha_dum
    Mar 2, 2014 at 15:47
  • No, sorry i just jumped into conclusions too early, one of my grandchildren category was empty so i just added 'hide_empty' => 0 into get_categories to show missing category.
    – lonerunner
    Mar 2, 2014 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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