Create columns
Creating a Custom Column
use PostTypes\Column;
class PriceColumn extends Column
{
/**
* Defines the column key used internally.
*
* @return string.
*/
public function name(): string
{
return 'price';
}
/**
* Define the column label.
*
* @return string
*/
public function label(): string
{
return __( 'Price', 'my-text-domain' );
}
/**
* Position a column before/after another.
*
* @return array
*/
public function position(): array
{
return $this->after( 'title' );
}
/**
* Populate column callback.
*
* @return callable
*/
public function populate(): callable
{
return function( int $post_id ) {
echo '$' . get_post_meta( $post_id, '_price', true );
};
}
/**
* Handle sorting the column by modifying the admin query.
*
* @return callable
*/
public function sort(): callable
{
return function( \WP_Query $query ) {
$query->set( 'meta_key', '_price' );
$query->set( 'orderby', 'meta_value_num' );
};
}
}Adding the Column to a Post Type
Last updated