wp_insert_post自定义分类输入到多个帖子

时间:2018-02-13 16:30:35

标签: php html forms post

我有一个前端表单,它将从一个表单创建两个单独的帖子。我的问题是在为每个帖子设置自定义分类时,它只会输入第一个选择而不是完整的选择数组。

如果我从$ services = $ taxinput [“job_listing_category”] [$ key];中删除[$ key],它会将两个输入中的所有分类选择添加到两个帖子中。

我无法解释为什么我只有一个分类标准选择仅应用于正确的个别帖子,或者我将两个输入中的所有分类选择添加到两个帖子中。

            if($_POST){
                foreach($_POST['class_name_1'] as $key =>$makepost) 
                {              

                    if(strlen($makepost)>3){

                        $post_id = -1;

                        $taxinput = $_POST["tax_input"];                       
                        $services = $taxinput["job_listing_category"][$key];

                        $class_name_1 = $_POST['class_name_1'][$key];

                        $post_id = wp_insert_post(
                                        array(
                                                'comment_status'  =>  'closed',
                                                'ping_status'   =>  'closed',
                                                'post_author'   =>  $author_id,
                                                'post_name'   =>  '',
                                                'post_title'    =>  '',
                                                'post_status'   =>  'pending',
                                                'post_type'   =>  'job_listing',
                                                'post_content'          =>      ''

                                        )
                                );   

                        wp_set_post_terms( $post_id, $services, 'job_listing_category', true);
                        wp_add_post_meta(  $post_id, 'class_name_1', $class_name_1);

                    }                        
                }
            }

这是我的前端输入表格:

 <form action="" method="post" id="submit-class" class="job-manager-form" enctype="multipart/form-data">


 <div class="class-name-edit">
       <label for="class_name">Class Name:</label>
            <div class="field ">
                   <?php 
                        get_job_manager_template( 'form-fields/text-field.php', 
                            array(
                               'key' => 'class_name_1[]', 
                               'field' => $all_fields['class_name_1']
                               )                        
                        );

                   ?>
            </div>
 </div>



<div class="service-category classes">
      <label for="job_category">Class categories:</label>
         </div>              
            <?php 
            get_job_manager_template( 'form-fields/term-checklist-field.php', 
                array( 
                    'key' => 'job_category[]', 
                    'field' => $all_fields['job_category']
                    )
                );
            ?>
         </div>

</div>


<div class="class-name-edit">
       <label for="class_name">Class Name:</label>
            <div class="field ">
                   <?php 
                        get_job_manager_template( 'form-fields/text-field.php', 
                            array(
                               'key' => 'class_name_1[]', 
                               'field' => $all_fields['class_name_1']
                               )                        
                        );

                   ?>
            </div>
 </div>



<div class="service-category classes">
      <label for="job_category">Class categories:</label>
         </div>              
            <?php 
            get_job_manager_template( 'form-fields/term-checklist-field.php', 
                array( 
                    'key' => 'job_category[]', 
                    'field' => $all_fields['job_category']
                    )
                );
            ?>
         </div>

</div>


  <div class="whitebox submitform">

  <input type="submit" name="submit_job" class="button"   value="Submit for Admin Approval" />

 </div>

</form>

0 个答案:

没有答案