简体   繁体   中英

ACF frontend form wordpress category not working

I am using a acf frontend form to create posts. All the fields are working but on frontend I want a field that should automatically asiign category at backend to the post.

I have custom post type name "person".

For adding the post from frontend form, here is the code

    // Create a new post
$post = array(
    'post_status'  => 'draft' ,
    'post_title'  => $_POST['fields']['field_53c8f0941cec0'] ,
    'post_category'  =>  array(43,47) ,
    'post_type'  => 'person' ,
    'submit_value'  => 'Submit' ,
);  

// insert the post
$post_id = wp_insert_post( $post ); 

. The custom taxonomy name for my "person" custom post type is "person_type" All the fields get saved but category does not gets saved at backend.

ANy help is really appreciated.

This should be pretty straight Use

'tax_input'      => array( 'CUSTOM_TAXONOMY_NAME' => array( COMMA SEPARTED VALUES OF TERMS)),

Example

  $post = array(
        'post_status'  => 'Pending' ,
        'post_title'  => $new_title ,
        'post_type'  => 'products' ,
        'tax_input'      => array( 'products_type' => array( 11,33)),
        'post_content' => $contentBlock,
        'submit_value'  => 'Submit' ,
    );  
$term = get_term_by( 'slug', 'your custom term slug', 'your custom taxonomy' );

$term_id = $term->term_id;

'tax_input' => array( 'your taxonomy' => $term_id )

this may help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM