自定义帖子类型排序管理员栏未找到

时间:2016-05-16 作者:TuKsn

下面的代码可以在appointment\\u start\\u date列之后进行排序,但如果单击该列的标题,所有约会都会消失(消息:找不到约会)。

代码如下:

add_action( \'init\',  \'custom_post_type\' );
add_action( \'add_meta_boxes\',  \'add_appointment_info_metabox\' );
add_filter( \'manage_edit-appointment_columns\',  \'custom_columns_head\', 10 );
add_filter( \'manage_edit-appointment_sortable_columns\',  \'sortable_columns\', 10 );
add_filter( \'pre_get_posts\', \'appointment_column_orderby\'); 
add_action( \'manage_posts_custom_column\',  \'custom_columns_display\', 10, 2 );
add_action( \'save_post\',  \'save_appointment_info\' );

// Register custom post type
function custom_post_type() {
    $labels = array(
        \'name\'                  =>   __( \'Appointments\', \'upcoming-appointments\' ),
        \'singular_name\'         =>   __( \'Appointment\', \'upcoming-appointments\' ),
        \'add_new_item\'          =>   __( \'Add New Appointment\', \'upcoming-appointments\' ),
        \'all_items\'             =>   __( \'All Appointments\', \'upcoming-appointments\' ),
        \'edit_item\'             =>   __( \'Edit Appointment\', \'upcoming-appointments\' ),
        \'new_item\'              =>   __( \'New Appointment\', \'upcoming-appointments\' ),
        \'view_item\'             =>   __( \'View Appointment\', \'upcoming-appointments\' ),
        \'not_found\'             =>   __( \'No Appointments Found\', \'upcoming-appointments\' ),
        \'not_found_in_trash\'    =>   __( \'No Appointments Found in Trash\', \'upcoming-appointments\' )
    );

    $supports = array(
        \'title\',
        \'editor\',
    );

    $args = array(
        \'label\'         =>   __( \'Appointments\', \'upcoming-appointments\' ),
        \'labels\'        =>   $labels,
        \'description\'   =>   __( \'A list of upcoming appointments\', \'upcoming-appointments\' ),
        \'public\'        =>   true,
        \'show_in_menu\'  =>   true,
        \'has_archive\'   =>   true,
        \'rewrite\'       =>   true,
        \'supports\'      =>   $supports
    );

    register_post_type( \'appointment\', $args );
}

// Metaboxes frontend
function render_appointment_info_metabox( $post ) {

    // generate a nonce field
    wp_nonce_field( basename( __FILE__ ), \'appointment-info-nonce\' );

    // get previously saved meta values (if any)
    $appointment_start_date = get_post_meta( $post->ID, \'appointment-start-date\', true );

    // if there is previously saved value then retrieve it, else set it to the current time
    $appointment_start_date = ! empty( $appointment_start_date ) ? $appointment_start_date : time();

    ?>

<label for="appointment-start-date"><?php _e( \'Appointment Start Date:\', \'upcoming-appointments\' ); ?></label>
        <input class="widefat appointment-date-input" id="appointment-start-date" type="text" name="appointment-start-date" value="<?php echo $appointment_start_date; ?>" required/>

  <?php 
}

// Add metaboxes to cpt
function add_appointment_info_metabox() {
    add_meta_box(
        \'appointment-info-metabox\',
        __( \'Appointment Info\', \'upcoming-appointments\' ),
         \'render_appointment_info_metabox\',
        \'appointment\',
        \'side\',
        \'core\'
    );
}

// Columns to show at the admin page
function custom_columns_head( $defaults ) {
    unset( $defaults[\'date\'] );

    $defaults[\'appointment_start_date\'] = __( \'Start Date\', \'upcoming-appointments\' );

    return $defaults;
}

// Admin page columns content
function custom_columns_display( $column_name, $post_id ) {
    if ( \'appointment_start_date\' == $column_name ) {
        $start_date = get_post_meta( $post_id, \'appointment-start-date\', true );
        echo $start_date;
    }
}

// Make columns sortable
function sortable_columns( $columns ) {
    $columns[\'appointment_start_date\'] = \'appointment_start_date\';

    return $columns;
}

// Custom sort order for column
function appointment_column_orderby( $query ) {  
       if( ! is_admin() )  
            return;  

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

        if( \'appointment_start_date\' == $orderby ) {  
            $query->set(\'meta_key\',\'appointment_start_date\'); 
            $query->set(\'orderby\',\'meta_value_num\'); 
        } 

        return $query;
} 

function save_appointment_info( $post_id ) {
    // checking for the \'save\' status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[\'appointment-info-nonce\'] ) && ( wp_verify_nonce( $_POST[\'appointment-info-nonce\'], basename( __FILE__ ) ) ) ) ? true : false;

    // exit depending on the save status or if the nonce is not valid
    if ( $is_autosave || $is_revision || ! $is_valid_nonce ) {
        return;
    }

    // checking for the values and performing necessary actions
    if ( isset( $_POST[\'appointment-start-date\'] ) ) {
        update_post_meta( $post_id, \'appointment-start-date\', $_POST[\'appointment-start-date\']  );
    }
}
标题后的默认排序:enter image description here

开始日期后排序:enter image description here

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

因为您将数据保存为appointment-start-date (连字符),但在orderby处理程序中使用appointment_start_date (下划线)。

他们需要匹配!

 $query->set( \'meta_key\', \'appointment-start-date\' ); 

相关推荐