get_post_meta in admin columns

Support General get_post_meta in admin columns

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #5407
    MD3MD3
    Participant

    I tried to add a custom column using get_post_meta but its not displaying?

    class Prefix_Custom_Admin_Columns extends MB_Admin_Columns_Post {
    	public function columns($columns) {
    		$columns = parent::columns($columns);
    		/**
    		 * Add more column in a specific position
    		 *
    		 * @param string $position New column position. Empty to not specify the position. Could be 'before', 'after' or 'replace'
    		 * @param string $target The target column. Used with combination with $position
    		 */
    		$this->add($columns, 'col_category', 'Category', 'after', 'title');
    		$this->add($columns, 'col_featured_image', 'Image', 'after', 'col_category');
    		return $columns;
    	}
    	public function show($column, $post_id){
    		switch($column){
    			case 'col_featured_image':
    				// This Works fine
    				the_post_thumbnail(array(70,70));
    				// This doesn't work
    				// echo get_the_post_thumbnail(array(70,70))
    			break;
    			case 'col_category':
    				// Not Displaying
    				echo get_post_meta($post_id, 'brn_category', true);
    			break;
    		}
    	}
    }

    even i tried to echo get_the_post_thumbnail() is not displaying too

    #5408
    MD3MD3
    Participant

    If i go with these settings in metabox. it will work but i don't want a link that's why i just want to display the string only. Not the one with anchor tag. Is there a way?

    array(
    	'name'        => 'Category',
    	'id'          => $prefix.'category',
    	'placeholder' => 'Select category',
    	'type'        => 'taxonomy',
    	'taxonomy'    => 'brand-category',
    	'field_type'  => 'select',
    	'query_args'  => array(
    		'orderby' => 'count',
    		'hide_empty' => 0
    	),
    	// 'admin_columns' => 'after title'
    ),
    #5424
    Anh TranAnh Tran
    Keymaster

    There are 2 things in you code:

    1. get_the_post_thumbnail needs the 1st param as the post ID. You can pass $post_id to the function, or simply null.
    2. The taxonomy field doesn't save value into post meta. Instead, it set post terms. So you need function get_the_terms to get them.

    Here is the working code: https://pastebin.com/txqnp0A3

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.