0

Im trying to understand how to get the custom field incr_number into admin columns. However im failing to understand and im googling but not finding a direct answer.

It cant be that hard surly?

   <?php


   /*-----------------------------------------------------------------------------------*/
   /* WooThemes supportpress Custom Post Types Init */
   /*-----------------------------------------------------------------------------------*/

  function woo_supportpress_post_types_init() {

    add_option('tickets_slug', 'tickets');

 register_post_type( 'ticket',
    array(
        'label' => __( 'Ticket', 'woothemes' ),
        'labels' => array(
            'name' => __( 'Tickets', 'woothemes' ),
            'singular_name' => __( 'Ticket', 'woothemes' ),
            'all_items'   => __( 'All Tickets', 'woothemes' ),
            'add_new' => __( 'Add New', 'woothemes' ),
            'add_new_item' => __( 'Add New Ticket', 'woothemes' ),
            'edit' => __( 'Edit', 'woothemes' ),
            'edit_item' => __( 'Edit Ticket', 'woothemes' ),
            'new_item' => __( 'New Ticket', 'woothemes' ),
            'view' => __( 'View Tickets', 'woothemes' ),
            'view_item' => __( 'View Ticket', 'woothemes' ),
            'search_items' => __( 'Search Tickets', 'woothemes' ),
            'not_found' => __( 'No tickets found', 'woothemes' ),
            'not_found_in_trash' => __( 'No tickets found in trash', 'woothemes' ),
            'parent' => __( 'Parent Tickets', 'woothemes' ),
        ),
        'description' => __( 'This is where you can create new tickets for your site. Tickets can also be created from the front-end.', 'woothemes' ),
        'public' => true,
        'menu_position' => 4,
        'show_ui' => true,
        'capability_type' => 'post',
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => get_option('tickets_slug'), 'with_front' => false ),
        'query_var' => true,
        'has_archive' => 'tickets',
        'supports' => array( 'title', 'editor', 'custom-fields', 'author', 'comments' ),
    )
);



register_taxonomy( 'ticket_status',
    array('ticket'),
    array(
        'hierarchical' => true,
        'labels' => array(
            'name' => __( 'Ticket Statuses', 'woothemes'),
            'singular_name' => __( 'Ticket Status', 'woothemes'),
            'search_items' =>  __( 'Search Ticket Statuses', 'woothemes'),
            'all_items' => __( 'All Ticket Statuses', 'woothemes'),
            'parent_item' => __( 'Parent Ticket Status', 'woothemes'),
            'parent_item_colon' => __( 'Parent Ticket Status:', 'woothemes'),
            'edit_item' => __( 'Edit Ticket Status', 'woothemes'),
            'update_item' => __( 'Update Ticket Status', 'woothemes'),
            'add_new_item' => __( 'Add New Ticket Status', 'woothemes'),
            'new_item_name' => __( 'New Ticket Status', 'woothemes')
        ),
        'public' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'ticket-status', 'with_front' => false ),
    )
);

register_taxonomy( 'ticket_priority',
    array('ticket'),
    array(
        'hierarchical' => true,
        'labels' => array(
                'name' => __( 'Ticket Priorities', 'woothemes'),
                'singular_name' => __( 'Ticket Priority', 'woothemes'),
                'search_items' =>  __( 'Search Ticket Priorities', 'woothemes'),
                'all_items' => __( 'All Ticket Priorities', 'woothemes'),
                'parent_item' => __( 'Parent Ticket Priority', 'woothemes'),
                'parent_item_colon' => __( 'Parent Ticket Priority:', 'woothemes'),
                'edit_item' => __( 'Edit Ticket Priority', 'woothemes'),
                'update_item' => __( 'Update Ticket Priority', 'woothemes'),
                'add_new_item' => __( 'Add New Ticket Priority', 'woothemes'),
                'new_item_name' => __( 'New Ticket Priority', 'woothemes')
        ),
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'ticket-priority', 'with_front' => false ),
    )
);

register_taxonomy( 'ticket_type',
    array('ticket'),
    array(
        'hierarchical' => true,
        'labels' => array(
                'name' => __( 'Ticket Types', 'woothemes'),
                'singular_name' => __( 'Ticket Type', 'woothemes'),
                'search_items' =>  __( 'Search Ticket Types', 'woothemes'),
                'all_items' => __( 'All Ticket Types', 'woothemes'),
                'parent_item' => __( 'Parent Ticket Type', 'woothemes'),
                'parent_item_colon' => __( 'Parent Ticket Type:', 'woothemes'),
                'edit_item' => __( 'Edit Ticket Type', 'woothemes'),
                'update_item' => __( 'Update Ticket Type', 'woothemes'),
                'add_new_item' => __( 'Add New Ticket Type', 'woothemes'),
                'new_item_name' => __( 'New Ticket Type', 'woothemes')
        ),
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'ticket-type', 'with_front' => false ),
    )
);

register_taxonomy( 'ticket_tags',
    array('ticket'),
    array(
        'hierarchical' => false,
        'labels' => array(
                'name' => __( 'Ticket Tags', 'woothemes'),
                'singular_name' => __( 'Ticket Tags', 'woothemes'),
                'search_items' =>  __( 'Search Ticket Tags', 'woothemes'),
                'all_items' => __( 'All Ticket Tags', 'woothemes'),
                'parent_item' => __( 'Parent Ticket Tag', 'woothemes'),
                'parent_item_colon' => __( 'Parent Ticket Tag:', 'woothemes'),
                'edit_item' => __( 'Edit Ticket Tag', 'woothemes'),
                'update_item' => __( 'Update Ticket Tag', 'woothemes'),
                'add_new_item' => __( 'Add New Ticket Tag', 'woothemes'),
                'new_item_name' => __( 'New Ticket Tag', 'woothemes')
        ),
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'ticket-tag' ),
    )
);


  }  

  add_action( 'init', 'woo_supportpress_post_types_init', 0 );


 /*-----------------------------------------------------------------------------------*/
 /* Admin columns for post types */
 /*-----------------------------------------------------------------------------------*/

 function woo_supportpress_ticket_columns($old_columns){

$columns = array(

'cb' => '<input type=\"checkbox\" />',
'title' => __('Ticket Title', 'woothemes'),
'incr_number' => __('Ticket ID', 'woothemes'),
'status' => __('Status', 'woothemes'),
'priority' => __('Priority', 'woothemes'),
'type' => __('Type', 'woothemes'),
'author' => __('Submitted by', 'woothemes'),
'assigned' => __('Assigned to', 'woothemes'),
'comments' => $old_columns["comments"],
'date' => __('Date', 'woothemes'),
);

return $columns;

 }

add_action( 'custom_woo_supportpress_ticket_columns',    'custom_woo_supportpress_ticket_columns', 10, 2 );

function custom_woo_supportpress_ticket_columns( $column, $post_id ) {
global $post;

switch( $column ) {

    /* If displaying the 'incr_number' column. */
    case 'incr_number' :

        /* Get the post meta. */
        $incr_number = get_post_meta($post_id, 'incr_number', true );



        break;

}
 }

 add_filter('manage_edit-ticket_columns', 'woo_supportpress_ticket_columns');

 function woo_supportpress_ticket_custom_columns($column) {
global $post;

$ticket_details = woo_supportpress_get_ticket_details( $post->ID );

switch ($column) {
    case "status" :
        echo get_the_term_list($post->ID, 'ticket_status', '', ', ','');
    break;
    case "priority" :
        echo get_the_term_list($post->ID, 'ticket_priority', '', ', ','');
    break;
    case "type" :
        echo get_the_term_list($post->ID, 'ticket_type', '', ', ','');
    break;
    case "assigned" :

        if ($ticket_details['assigned_to']->ID>0) :
            $link = get_author_posts_url( $ticket_details['assigned_to']->ID );
        else :
            $link = add_query_arg('assigned_to', '0', get_post_type_archive_link('ticket'));
        endif;

        echo '<a href="'.$link.'">'.$ticket_details['assigned_to']->display_name.'</a>';

    break;
    case "id" :
        echo '#'.$post->ID;
    break;
}
 }
add_action('manage_ticket_posts_custom_column', 'woo_supportpress_ticket_custom_columns', 2);
7
  • can you clarify exactly where this code is? i.e. is it inside a function that you're calling from a hook? May 11, 2013 at 21:00
  • Its called by the function.php require_once locate_template('/lib/ticket-post-type.php'); // Custom Post Types May 11, 2013 at 21:02
  • try reading and working your way through this tutorial, you should be able to find the relevant functions in your theme and add in your extra column. May 11, 2013 at 21:12
  • Hi I tried that one earlier, but maybe i was doing it wrong? May 11, 2013 at 21:22
  • is the first part of your code (where you define the array) inside a function, and is that function called by a hook? in your question it's just floating around in the middle of nowehere... May 11, 2013 at 21:27

1 Answer 1

1

Try replacing everything from the comment downwards with this. Basically you seemed to have a function in there that wasn't getting called, and you need to add the case "incr_number" test to the woo_supportpress_ticket_custom_columns function:

/*-----------------------------------------------------------------------------------*/
/* Admin columns for post types */
/*-----------------------------------------------------------------------------------*/

function woo_supportpress_ticket_columns( $old_columns ){

    $columns = array(
        'cb'          => '<input type=\"checkbox\" />',
        'title'       => __('Ticket Title', 'woothemes'),
        'incr_number' => __('Ticket ID', 'woothemes'),
        'status'      => __('Status', 'woothemes'),
        'priority'    => __('Priority', 'woothemes'),
        'type'        => __('Type', 'woothemes'),
        'author'      => __('Submitted by', 'woothemes'),
        'assigned'    => __('Assigned to', 'woothemes'),
        'comments'    => $old_columns["comments"],
        'date'        => __('Date', 'woothemes'),
        );

    return $columns;

}

add_filter('manage_edit-ticket_columns', 'woo_supportpress_ticket_columns');

function woo_supportpress_ticket_custom_columns( $column ) {

    global $post;

    $ticket_details = woo_supportpress_get_ticket_details( $post->ID );

    switch ($column) {
        case "incr_number" :
            echo get_post_meta( $post->ID, 'incr_number', true );
        break;
        case "status" :
            echo get_the_term_list($post->ID, 'ticket_status', '', ', ','');
        break;
        case "priority" :
            echo get_the_term_list($post->ID, 'ticket_priority', '', ', ','');
        break;
        case "type" :
            echo get_the_term_list($post->ID, 'ticket_type', '', ', ','');
        break;
        case "assigned" :
            if ($ticket_details['assigned_to']->ID>0) :
                $link = get_author_posts_url( $ticket_details['assigned_to']->ID );
            else :
                $link = add_query_arg('assigned_to', '0', get_post_type_archive_link('ticket'));
            endif;
            echo '<a href="'.$link.'">'.$ticket_details['assigned_to']->display_name.'</a>';
        break;
        case "id" :
            echo '#'.$post->ID;
        break;
    }
}

add_action('manage_ticket_posts_custom_column', 'woo_supportpress_ticket_custom_columns', 2);
3
  • hi thanks for that but it made no difference, incr_number still isn't been populated. it displays fine in loops so i know it works just cant figure out why not on the back end May 11, 2013 at 22:46
  • ` echo get_post_meta( $post->ID, 'incr_number', true );` THIS FIXES THE PROBLEM! Thanks Simon Blackbourn your a life saver! May 11, 2013 at 22:50
  • glad to help! well spotted with the typo - i've now updated my code with your correction. May 12, 2013 at 15:50

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.