星期四 , 23 1 月 2025

如何添加字段到产品文章管理列并且实现可以排序

//首先添加pertnumber到product管理列

add_filter('manage_edit-product_columns','add_partnumber_to_admincolumn');

function add_partnumber_to_admincolumn($columns) {
    
    //add a new column after title
    $columns['partnumber'] = 'Part Number'; 
    
    return $columns;
    
}

//在管理列显示想要的内容
add_action('manage_product_posts_custom_column','display_partnumber_column',10,2);

function display_partnumber_column($column,$post_id) {
    if('partnumber' == $column) {
        $partnumber = get_post_meta($post_id,'partnumber',true);
        echo $partnumber ? esc_html($partnumber) : '-';
    }
    
}

//实现这个管理列的排序 

//make the column sortable  记住这个钩子 manage_edit-product_sortable_columns

add_action('manage_edit-product_sortable_columns','make_part_number_column_sortable');

function make_part_number_column_sortable($columns) {
    
    $columns['partnumber'] = 'Part Number';
    
    return $columns;
    
}

//pre get posts 调整排序 

add_action('pre_get_posts','part_number_column_orderby'); 

function part_number_column_orderby($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }
    
    //如果点击了这一列的排序按钮  就会执行一个新的wp query的查询,  所以就会存在这个$query 
    if ('partnumber' === $query->get('orderby') ) {
        $query->set('meta_key','partnumber');
        $query->set('orderby','meta_value');
    }
    
}

怎样查看列的key名字

function debug_product_columns($columns) {
    print_r($columns); // Outputs the column keys and labels
    return $columns;
}
add_filter('manage_product_posts_columns', 'debug_product_columns');

完整的示范代码

// Add custom columns for Serial Number and Model taxonomy
function add_custom_columns($columns) {
    $columns['serial_number'] = 'Serial Number'; // Add Serial Number column
    $columns['model'] = 'Model';                // Add Model taxonomy column
    return $columns;
}
add_filter('manage_edit-product_columns', 'add_custom_columns');

// Populate the custom columns
function populate_custom_columns($column, $post_id) {
    if ('serial_number' === $column) {
        // Display the _serial_number meta field value
        $serial_number = get_post_meta($post_id, '_serial_number', true);
        echo $serial_number ? esc_html($serial_number) : '-'; // Show dash if empty
    }

    if ('model' === $column) {
        // Display the assigned Model taxonomy terms
        $terms = get_the_terms($post_id, 'model');
        if (!empty($terms) && !is_wp_error($terms)) {
            $term_names = wp_list_pluck($terms, 'name');
            echo esc_html(implode(', ', $term_names)); // Display terms as a comma-separated list
        } else {
            echo '-'; // Show dash if no terms are assigned
        }
    }
}
add_action('manage_product_posts_custom_column', 'populate_custom_columns', 10, 2);

// Make the columns sortable (optional)
function make_custom_columns_sortable($columns) {
    $columns['serial_number'] = '_serial_number'; // Sort by serial number meta
    $columns['model'] = 'model';                 // Sort by Model taxonomy
    return $columns;
}
add_filter('manage_edit-product_sortable_columns', 'make_custom_columns_sortable');

// Handle sorting logic (optional)
function handle_custom_columns_sorting($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

    $orderby = $query->get('orderby');

    if ('_serial_number' === $orderby) {
        $query->set('meta_key', '_serial_number');
        $query->set('orderby', 'meta_value');
    }

    if ('model' === $orderby) {
        $query->set('orderby', 'taxonomy');
        $query->set('taxonomy', 'model');
    }
}
add_action('pre_get_posts', 'handle_custom_columns_sorting');