I'm collecting meta key data, and I want to display it in the WordPress dashboard

我的梦境 提交于 2020-04-18 05:46:03

问题


I found, and modified this for my needs, and put it in my functions.php:

add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );

    //edit this for your column(s)
    //all of your columns will be added before the actions column
    $new_columns['dname'] = 'Dogs Name';
    $new_columns['additional_allergies'] = 'Allergies';

    //stop editing
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
    return $new_columns;
}

and in my store checkout I collect these two meta keys: "dname" and "additional_allergies"


回答1:


Assuming the meta_keys used = dname and additional_allergies. If this is not the case, you will see the 'Meta key is wrong' message, then it is a matter of looking for the right meta_key in your database.

// Add a Header
function custom_shop_order_column( $columns ) {
    // Add new columns
    $columns['dogs_name'] = 'Dogs Name';
    $columns['additional_allergies'] = 'Allergies';

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );

//  Populate the Column
function custom_shop_order_list_column_content( $column, $post_id ) {
    // Compare
    if ( $column == 'dogs_name' ) {
        $dogs_name = get_post_meta( $post_id, 'dname', true);

        if ( $dogs_name ) {
            echo $dogs_name;
        } else {
            echo 'Meta key is wrong';
        }
    }

    if ( $column == 'additional_allergies' ) {
        $allergies = get_post_meta( $post_id, 'additional_allergies', true);

        if ( $allergies ) {
            echo $allergies;
        } else {
            echo 'Meta key is wrong';
        }       
    }   
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );


来源:https://stackoverflow.com/questions/61154720/im-collecting-meta-key-data-and-i-want-to-display-it-in-the-wordpress-dashboar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!