WordPress自定义帖子类型出席元框

时间:2019-09-15 16:16:29

标签: php database wordpress custom-post-type post-meta

我创建了一个自定义帖子类型,称为“出勤”。

attendance CPT Screen

添加新的出勤时,我使用 get_users()函数来抓取所有用户(具有角色订阅者)并在每个用户的表中打印出新行。

这是我的中继框渲染功能:

public function render_features_box($post) {
    wp_nonce_field( 'custom_attendance', 'custom_attendance_nonce' );

    $data = get_post_meta( $post->ID, '_custom_attendance_key', true );

    $attendee = isset($data['attendee']) ? $data['attendee'] : '';
    $attendance_date = isset($data['attendance-date']) ? $data['attendance-date'] : '';
    $attendance = isset($data['attendance']) ? $data['attendance'] : null;

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

   ?>

    <p> 
        <label for="custom-attendance-date"><?php _e( 'Attendance Date:', 'custom' ); ?></label>
        <input type="text" id="custom-attendance-date" name="custom-attendance-date" class="widefat" value="<?php echo date( 'F d, Y', $attendance_date ); ?>" placeholder="Format: December 18, 2020">
    </p>

    <p>
        <table class="form-table">
            <thead>
                <tr>
                    <th>User</th>
                    <th scope="col">Attendance</th>
                </tr>
            </thead>
            <tbody>
            <?php
            $args = array(
                'role'    => 'subscriber',
                'orderby' => 'user_nicename',
                'order'   => 'ASC'
            );
            $radio = 0;
            $users = get_users( $args );
            foreach($users as $user){ ?>             
                <tr>
                    <td>
                    <input type="hidden" id="custom-attendee" name="custom-attendee[]" class="widefat" value="<?php echo esc_html( $user->display_name ) ?>" >
                    <?php echo esc_html( $user->display_name )  ?>
                    </td>
                    <td>
                        <label>Present</label>
                        <input type="radio" id="attendance_status" name="attendance_status[<?php echo $radio; ?>]" value="Present" <?php checked( $attendance, 'Present' ); ?> />
                        <label>Absent </label>
                        <input type="radio" id="attendance_status" name="attendance_status[<?php echo $radio; ?>]" value="Absent" <?php checked( $attendance, 'Absent' ); ?> />
                    </td>
                </tr>
                <?php
                $radio++;
                }
            ?>
            </tbody>
        </table>
    </p>
  <?php
}

我正在使用以下功能保存考勤职位:

public function save_meta_box($post_id) {
    if (! isset($_POST['custom_attendance_nonce'])) {
        return $post_id;
    }

    $nonce = $_POST['custom_attendance_nonce'];
    if (! wp_verify_nonce( $nonce, 'custom_attendance' )) {
        return $post_id;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if (! current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }


    $data = array(
        'attendance-date' => strtotime( $_POST['custom-attendance-date'] ),
        'attendee' => isset($_POST['custom-attendee']) ? $_POST['custom-attendee'] : '',
        'attendance' => isset($_POST['attendance_status']) ? $_POST['attendance_status'] : null
    );

    update_post_meta( $post_id, '_custom_attendance_key', $data );
}

我遇到的问题是序列化时数据库(wp_postmeta)中的数据不是必需的,我需要将其作为日期,然后列出每个用户的出席情况,无论其出席与否。

这是将数据保存在post_meta表中的方式:

a:3:{s:15:"attendance-date";i:1569024000;s:8:"attendee";a:11:{i:0;s:5:"Anais";i:1;s:7:"Rashawn";i:2;s:9:"Rosalinda";i:3;s:5:"Sarah";i:4;s:3:"Edd";i:5;s:4:"Tina";i:6;s:4:"Asha";i:7;s:4:"Jose";i:8;s:7:"Jacinto";i:9;s:12:"Test Testing";i:10;s:5:"Betty";}s:10:"attendance";a:11:{i:0;s:7:"Present";i:1;s:7:"Present";i:2;s:6:"Absent";i:3;s:7:"Present";i:4;s:6:"Absent";i:5;s:7:"Present";i:6;s:6:"Absent";i:7;s:7:"Present";i:8;s:6:"Absent";i:9;s:7:"Present";i:10;s:6:"Absent";}}

此外,当我在admin中加载帖子时,不检查单选按钮的特定值(存在或不存在)。我不知道单选按钮阵列的设置是否正确,我已经尝试了好几个小时,却无所适从。

这种数据适合保存在Post_meta中还是在自定义表中处理起来容易得多?

0 个答案:

没有答案