• Resolved baburman

    (@baburman)


    Hi,

    I have created a Custom Post Type ‘ads’, which is working fine on both admin and user end. Now I have to create a couple of columns in the CPT default listing page in WP admin.

    I have created label for the fields like this:

    function adm_ads_table_head( $defaults ) {
    $defaults[‘ad_expire_date’] = ‘Expiry Date’;
    $defaults[‘ad_clicks’] = ‘Clicks’;
    return $defaults;
    }

    and the function is hooked like this:

    add_filter(‘manage_ads_posts_columns’, array( $this, ‘adm_ads_table_head’));

    so far so good. Now I have to set content for the above columns. I have used this simple function to make it work.

    function adm_ads_table_content( $column_name, $post_id ) {
    if ($column_name == ‘ad_expire_date’) {
    //echo ‘121212’;
    //echo get_metadata(‘ads’, $post_id, ‘_ad_dtexp’, true);
    echo get_post_meta( $post_id, ‘_ad_dtexp’, true );
    }
    }

    and this is the hook:

    add_action( ‘manage_ads_posts_custom_column’, array( $this, ‘adm_ads_table_content’) );

    The problem is when i echo any static value like ‘121212’ it successfully shows in the Expiry Date column. But when i try to use

    get_metadata or get_post_metadata, nothing displays in that column. Please help!!!

    baburman

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter baburman

    (@baburman)

    Looks like to me the problem lies on this line:

    add_action( ‘manage_ads_posts_custom_column’, array( $this, ‘adm_ads_table_content’)

    the function adm_ads_table_content is supposed to receive 2 parameters. Since I am writing this code in a class. If it were not within the class definition it would have been like:

    add_action( ‘manage_ads_posts_custom_column’, ‘adm_ads_table_content’, 10, 2)

    I don’t know how will i specify the arguments in this particular case. Substantial googling didn’t provide any help either.

    baburman

    Moderator bcworkz

    (@bcworkz)

    add_action( 'manage_ads_posts_custom_column', array( $this, 'adm_ads_table_content'), 10, 2 );
    ??

    Thread Starter baburman

    (@baburman)

    gr8. It works!!!

    thanks bcworkz ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add columns in a Custom Post Type list in Admin.’ is closed to new replies.