从前端表单以分层术语的形式插入类别

时间:2015-07-12 作者:Rohit Poonia

我想插入或您可以说从我的前端表单中选择自定义帖子类型中的预定义类别。我不知道我在哪里丢失了任何代码。请帮我解决这个问题。。。

下面是我的代码。提前谢谢!!

<?php
global $wpdb;
$user_id = get_current_user_id();
 if(!empty($user_id)){

    $jaddress = wp_strip_all_tags( $_POST[\'jobaddress\'] );
    $jobcity = wp_strip_all_tags( $_POST[\'jobcity\'] );
    $jobpostal = wp_strip_all_tags( $_POST[\'jobpostal\'] );
    $jobmobile = wp_strip_all_tags( $_POST[\'jobmobile\'] );
    $post_information = array(
    \'post_title\' => wp_strip_all_tags( $_POST[\'jobtitle\'] ),
    \'post_content\' => $_POST[\'jobdescription\'],
    \'post_category\' => array($_POST[\'jobtype\']),
    \'post_type\' => \'employment\',
    \'post_author\' => $user_id,
    \'post_status\' => \'pending\'
    );

    $pid =  wp_insert_post( $post_information );
    wp_set_post_terms($pid,array($_POST[\'jobtype\']),\'jobtype\',true);
    add_post_meta($pid, \'wpcf-jobaddress\', $jaddress, true);    
    add_post_meta($pid, \'wpcf-jobparishcity\', $jobcity, true);    
    add_post_meta($pid, \'wpcf-jobpostalcode\', $jobpostal, true);    
    add_post_meta($pid, \'wpcf-jobmobilenumber\', $jobmobile, true);    

}else{

   $jaddress = wp_strip_all_tags( $_POST[\'jobaddress\'] );
   $jobcity = wp_strip_all_tags( $_POST[\'jobcity\'] );
   $jobpostal = wp_strip_all_tags( $_POST[\'jobpostal\'] );
   $jobmobile = wp_strip_all_tags( $_POST[\'jobmobile\'] );
   $post_information = array(
   \'post_title\' => wp_strip_all_tags( $_POST[\'jobtitle\'] ),
   \'post_content\' => $_POST[\'jobdescription\'],
   \'post_category\' => array($_POST[\'jobtype\']),
   \'post_type\' => \'employment\',
   \'post_status\' => \'pending\'
    );

$pid =  wp_insert_post( $post_information );
add_post_meta($pid, \'wpcf-jobaddress\', $jaddress, true);    
add_post_meta($pid, \'wpcf-jobparishcity\', $jobcity, true);    
add_post_meta($pid, \'wpcf-jobpostalcode\', $jobpostal, true);    
add_post_meta($pid, \'wpcf-jobmobilenumber\', $jobmobile, true);

}?> 

    <div class="doublefield">
        <p><span class="select-style"><?php wp_dropdown_categories( \'show_option_none=Job Type&taxonomy=jobtype\' ); ?><!--<select id="jobtype" name="jobtype"><option value="Permanent">Permanent</option><option value="Temporary">Temporary</option></select>--></span></p>
        <p><span class="select-style"><select id="joblocation" name="joblocation"><option value="United States">United States</option><option value="India">India</option><option value="China">China</option></select></span></p>
    </div>

1 个回复
SO网友:Rituparna sonowal

使用wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); 来完成这件事。

您正在添加所有内容add_post_meta 不会将类别添加到您的帖子类型。

    wp_set_object_terms( $post_id, sanitize_text_field($_POST[\'some_field\'] ), \'schooltype\', true );
但在这里,我也不知道如何通过

wp_set_object_terms, 甚至WordPress codex上也没有提到!我认为您应该使用:

wp_insert_term( $term, $taxonomy, $args = array() ); .

您还可以检查以下链接:

https://codex.wordpress.org/Function_Reference/wp_insert_term

How to set hierarchical terms to a post using wp_set_object_terms

结束

相关推荐

GET_CATEGORIES()返回“未分类”

在我的WPMU网站前端,所有类别都消失了,每个帖子都标有“未分类”。然而,这只是单曲。php,而不是在主页上。php。相同的错误发生在get_categories(), wp_list_categories() 和the_category().我试着用var_dump(get_categories()) 但就像WordPress一样,它只是认为应该这样。