WordPress 自定义 Post 类型 FrontEnd

WordPress Custom Post Type FrontEnd

我创建了一个自定义 post 类型 'student',现在我希望学生可以通过前端表单提交他们的详细信息。但是我已经使用年份和部门作为自定义分类法,我可以在前端将它们显示为下拉列表,但不能将字段值提交给关于分类法。我的代码如下:

    <?php add_shortcode( 'singul_frontend_post', 'singul_frontend_post' );
function singul_frontend_post() {
    singul_save_post_if_submitted();
    
    ?>
<div id="postbox">
    <form id="new_post" name="new_post" method="post" >

    <div class="form-group"><label for="title">Student Name</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="title" class="form-control" />
</div>
<div class="row">
    <div class="col-md-6">
        <div class="form-group">
        <label for="title">Year Of SSC</label><br />
    <select name="ssc" class="form-control" searchable="A">
    <?php
       $tax_terms = get_terms('ssc', array('hide_empty' => '0'));      
       foreach ( $tax_terms as $tax_term ):  
          echo '<option value="" selected disabled hidden>Choose Year</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';   
       endforeach;
    ?>
</select></div>
</div>
<div class="col-md-6">
    <div class="form-group">
    <label for="title">Group</label><br />
    <select name="group" class="form-control" searchable="A">
    <?php
       $tax_terms = get_terms('group', array('hide_empty' => '0'));      
       foreach ( $tax_terms as $tax_term ):  
          echo '<option value="" selected disabled hidden>Choose Group</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';   
       endforeach;
    ?>
</select></div></div>
</div>

<div class="row">
    <div class="col-md-6">
    <div class="form-group"><label for="title">Current Position</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="cposition" class="form-control" />
    </div>
    </div>
    <div class="col-md-6"><div class="form-group"><label for="title">Current Institute</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="cinstitute" class="form-control" />
    </div></div>
</div>

    <div class="row">
        <div class="col-md-6">
        <div class="form-group"><label for="title">Contact Number</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="contact" class="form-control"  placeholder="01XXXXXXXXX"/>
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group"><label for="title">Email Address</label><br />
        <input type="email" id="title" value="" tabindex="1" size="20" name="email" class="form-control"  />
    </div>
    </div>
</div>

    <div class="row">
        <div class="col-md-6">
        <div class="form-group"><label for="title">Facebook ID</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="facebook" class="form-control"  />
    </div>
        </div>
        <div class="col-md-6">
        <div class="form-group"><label for="title">Photo</label><br />
    <input type="file" name="user-image-featured" id="user-image-featured" size="20" class="form-control-file" >
    <?php wp_nonce_field( 'user-image-featured', 'user-image-featured_nonce' ); ?>
</div>
        </div>
    </div>
   
    
    
    <?php wp_nonce_field( 'wps-frontend-post' ); ?>

    <div class="form-group"><button type="submit" value="Publish" tabindex="6" id="submit" name="submit" class="btn btn-primary">Submit</button></div>
    
    </form>
</div>
    <?php
}

    
    function singul_save_post_if_submitted() {
    // Stop running function if form wasn't submitted
    if ( !isset($_POST['title']) ) {
        return;
    }

    // Check that the nonce was set and valid
    if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
        echo 'Did not save because your form seemed to be invalid. Sorry';
        return;
    }

    // Do some minor form validation to make sure there is content
    if (strlen($_POST['title']) < 3) {
        echo 'Please enter a title. Titles must be at least three characters long.';
        return;
    }
    

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $_POST['title'],
        'post_status'   => 'draft',   // Could be: publish
        'post_type'     => 'student' // Could be: `page` or your CPT
    );
    
    $cposition = $_POST['cposition'];
    $institute = $_POST['cinstitute'];
    $contact = $_POST['contact'];
    $email = $_POST['email'];
    $fbid = $_POST['facebook'];
    $ssc = $_POST['ssc'];
    $group = $_POST['group'];
    $post_id = wp_insert_post($post);
    update_post_meta( $post_id, 'snp_student_cposition', $cposition );
    update_post_meta( $post_id, 'snp_student_institute', $institute );
    update_post_meta( $post_id, 'snp_student_contact', $contact );
    update_post_meta( $post_id, 'snp_student_email', $email );
    update_post_meta( $post_id, 'snp_student_fbid', $fbid );
    wp_set_object_terms($post_id, $ssc, 'ssc');
    wp_set_object_terms($post_id, $group, 'group');
    
        // The nonce was valid and the user has the capabilities, it is safe to continue.
     
        // These files need to be included as dependencies when on the front end.
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
         
        // Let WordPress handle the upload.
        // Remember, 'user-image-featured' is the name of our file input in our form above.
        $attachment_id = media_handle_upload( 'user-image-featured', $post_id);
            set_post_thumbnail( $post_id, $attachment_id );
         
    echo 'Saved your post successfully! :)';
}

这里 singul_field_name 名称是元键。还在考虑如何获得特色图片的attachment_id

**更新: 所有自定义元插入已完成,只是无法插入 post 个特色图片。

您可以使用 wp_set_object_terms 功能来保存自定义分类术语。

wp_set_object_terms( $post_id, $_POST['input_name_of_your_dropdown'], 'ssc', true );
wp_set_object_terms( $post_id, $_POST['input_name_of_your_dropdown'], 'group', true );

上面的代码应该放在wp_insert_post之后,这样才能找到$post_id