Adding new columns for posts in the WordPress admin panel open

Adding new columns for posts in the WordPress admin panel

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

You can place this code in your functions.php or in your WordPress plugin


// manage_cars_posts_columns - instead of cars enter your post type
add_action('manage_cars_posts_columns', 'custom_colums_extended');
add_action('manage_cars_posts_custom_column', 'custom_colums_extended_data', 5, 1); // 1 - how many parameters we pass function
add_filter('manage_edit-cars_sortable_columns', 'custom_colums_extended_sort');
add_action('pre_get_posts', 'custom_colums_extended_sort_order');

function custom_colums_extended($columns) {

    //get the data of the columns

    $title = $columns['title'];
    $date = $columns['date'];

    //save the data of the columns
    $columns['title'] = $title;
    $columns['date'] = $date;

    //add a new column with the key price
    $columns['price'] = esc_html__('Price', 'myplugin');

    return $columns;

}

function custom_colums_extended_data($column, $post_id) {

    $price = get_post_meta($post_id, 'price', true);

    if ($column == 'price') {
        echo esc_html($price);
    }

}

// Add sorting for the Price column

function custom_colums_extended_sort($columns) {

    $columns['price'] = 'price';

    return $columns;

}

function custom_colums_extended_sort_order($query) {

}
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation