How to add Custom column for custom post type on backend

add_filter( 'manage_edit-cptx_business_columns', array($this, 'customColumns') ) ;

add_action( 'manage_cptx_business_posts_custom_column', array($this, 'myCustomColumns'), 10, 3 );

public function myCustomColumns($column, $post_id) {

        global $post;

        $user_ID = get_current_user_id();

        $count_user_posts_by_type= create_function('$userid,$meta_key, $meta_value, $post_type','

            global $wpdb;

            $where = " WHERE post_type = \'cptx_rental\' AND post_author = 4 AND postmeta.meta_value=\'$meta_value\' AND postmeta.meta_key = \'$meta_key\' ";

            $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts posts left join $wpdb->postmeta postmeta on(postmeta.post_id =posts.id) $where " );

            return apply_filters( \'get_usernumposts\', $count, $userid );

        ');

        $total_rentals = $count_user_posts_by_type($user_ID,"_cptx_rental_client_id",$post->ID,'cptx_rental');

        $total_business = $count_user_posts_by_type($user_ID,"_cptx_rental_client_id",$post->ID,'cptx_business');

        switch( $column ) {

            case 'business_type' :

                $terms = wp_get_post_terms( $post_id, 'cptx_business_listing_type' );

                echo $terms[0]->name;

                break;

            case 'num_businesses' :

                echo $total_business;

                break;

            case 'users':

                $client_id = get_post_meta($post_id, '_cptx_rental_client_id', true);

                $client_meta = get_post_meta( $client_id ,'_cl_first_name',true);

                echo $client_meta;

                break;

            /* Just break out of the switch statement for everything else. */

            default :

                break;

        }

    }

public function customColumns() {

        $columns = array(

            'cb' => '<input type="checkbox" />',

            'title' => __( 'Title' ),

            'business_type' => __( 'Business Type' ),           

            'thumb' => __( 'Thumb' ),

            'users' => __( 'Users' ),

            'date' => __( 'Date' ),

        );

        return $columns;

    }

Leave a Reply

Your email address will not be published.