0

I have a front end form, one of the inputs asks for a comma separated list of keywords which should each end up being a custom term, I just saw in the backend the terms are entered as single string with the commas and not individually.

My code is below, how can I turn the string ie; term1,term2,term3 which may be entered in the input to 'term1','term2','term3' which is what the format looks like it needs to be to insert each keyword as a unique term?

I've tried both with wp_insert_post and wp_set_object_terms

$keywords = $_POST['keywords'];

$new_post = array(
    'post_title'    => $title,
    'tax_input'    => array( 'keyword' => array( $keywords ) ),
    'post_status'   => 'publish',
    'post_type' => 'test'
);

$nonce = $_POST['nonce'];
 if ( ! wp_verify_nonce( $nonce, 'form-nonce' ) ) {
     die( 'Security check' ); 
 } else {
    $pid = wp_insert_post($new_post);
    //var_dump($new_post);

    //wp_set_object_terms($pid,$keywords,'keyword');
 }
    wp_redirect( get_permalink($pid) ); 
    //echo $pid;
    exit();
1

you need to make an array from comma separated string of keywords like

$keywords = explode(',', $keywords);

then your insert post data array will be.

$new_post = array(
  'post_title'    => $title,
  'tax_input'    => array( 'keyword' => $keywords ),
  'post_status'   => 'publish',
  'post_type' => 'test'
);

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.