0

I want to insert or you may say select the pre defined categories in my custom post type from my front end form. i don't know where i am missing any code. Please help me out in this ...

Below is my code. Thanks in advance !!

<?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>
0

Use wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); to get this done.

You are adding everything with add_post_meta which will not add category to your post type.

    wp_set_object_terms( $post_id, sanitize_text_field($_POST['some_field'] ), 'schooltype', true );

But here I am also not sure how to assign parent-children relationship via

wp_set_object_terms, even it's not mentioned on WordPress codex! I think you should use:

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

You can also check these links:

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

How to set hierarchical terms to a post using wp_set_object_terms

  • Hello Thanks for Replying ... i Got it and done .... now i have a query if you don't mind... how i am able to select two categories for a post below is the code.. <code>'post_author' => $user_id, 'post_status' => 'pending', 'post_category' => array($_POST['cat'],$_POST['joblocation']) //'taxonomy' => 'jobtype' $pid = wp_insert_post( $post_information ); wp_set_post_terms( $pid, $_POST['cat'], 'jobtype', false ); wp_set_post_terms( $pid, $_POST['joblocation'], 'jobscategory', false );]</code> – Rohit Poonia Jul 12 '15 at 6:21
  • Please accept the answer if it really helped you, because that only help people in future. Are you inserting or retrieving values .. If you are inserting then just use array with multiple terms(categories). Hope it will help you. – Rituparna sonowal Jul 12 '15 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.