• Resolved wzshop

    (@wzshop)


    I have this script to save a custom field as a custom taxonomy term. All is working fine, but I would like to extend it a little bit, so that multiple values in the same custom field (seperated by a comma) are saved as different custom taxonomy terms. In example: in a custom field i write ‘value 1, value 2’. I would like those 2 values to be separately saved as a custom taxonomy term. Is there anyone who has a solution for this, based on my code below?

    //Custom field to custom taxonomy script
        $input_terms = array();
        $input_terms[] = sanitize_text_field( $_POST['customfieldname1'] );
        $input_terms[] = sanitize_text_field( $_POST['customfieldname2'] );
        $terms = array();
    
        foreach( $input_terms as $term ) {
    
         $existent_term = term_exists( $term, 'mycustomtaxonomy' );
    
            if( $existent_term && $existent_term['term_id'] ) {
    
                $term_id = $existent_term['term_id'];
    
            } else {
    
        $term = wp_insert_term(
        $term, // the term
          'mycustomtaxonomy', // the taxonomy
          array(
            'description'=> 'This is '.$term.'!',
            'slug' => $term
          )
        );
    
        if( !is_wp_error($term ) && $term['term_id'] ) {
                     $term_id = $term['term_id'];
                } 
    
           }
        //Fill the array of terms for later use on wp_set_object_terms
           $terms[] = (int) $term_id;
    
        }
        wp_set_object_terms( $listing_id, $terms, 'mycustomtaxonomy' );
    reset();
Viewing 8 replies - 1 through 8 (of 8 total)
  • You could use the explode function to break up the input value like so:

    $input_1 = explode( ',' ,sanitize_text_field( $_POST['customfieldname1'] );
    $input_2 = explode( ',' ,sanitize_text_field( $_POST['customfieldname2'] );
    $input_terms = array_merge( $input_1 ,$input_2 );

    Thread Starter wzshop

    (@wzshop)

    awesome, thank you soooo much!
    ps; a ‘)’ was missing in the code, if someone else wants to use it!

    Thread Starter wzshop

    (@wzshop)

    Hi, sorry was a bit too enthusiastic. It is working, however I have a slightly different situation then given, which causes for it to break. I have 3 checkbox field that someone could (un)check. Then i also have a normal text field where someone could write in tags. See the code below. Now in this case only the values written in the textfield are saved and nog the ceckboxes:

    //Custom field to custom taxonomy script
    $input_terms = array();
    $input_terms[] = sanitize_text_field( $_POST['checkbox1'] );
    $input_terms[] = sanitize_text_field( $_POST['checkbox2'] );
    $input_terms[] = sanitize_text_field( $_POST['checkbox3'] );
    $input_1 = explode( ",", sanitize_text_field( $_POST['customfieldname1'] ));
    $input_terms = array_merge( $input_1 );
    $terms = array();

    How to fix this?
    Thanks

    Ok.. use this:

    //Custom field to custom taxonomy script
    $input_terms = array();
    $input_terms[] = sanitize_text_field( $_POST['checkbox1'] );
    $input_terms[] = sanitize_text_field( $_POST['checkbox2'] );
    $input_terms[] = sanitize_text_field( $_POST['checkbox3'] );
    $input_1 = explode( ",", sanitize_text_field( $_POST['customfieldname1'] ));
    $input_terms = array_merge( $input_1, $input_terms );
    $terms = array();

    Also remember to check the value of the post variable by using isset() check,so you don’t run into errors when any of the checkbox is not checked, and when the input field is empty. Also before doing a foreach on the $input_terms variable, also run a check to make sure it’s not empty.

    Thread Starter wzshop

    (@wzshop)

    Awesome, thanks.. will do.

    Thread Starter wzshop

    (@wzshop)

    Sorry, but just to be clear;

    a way to do the isset is to just call that function before the foreach function and close it after the wp_set_object_terms function? like

    <?php
    if(isset($input_terms)) {
    foreach( $input_terms as $term ) {
    
         $existent_term = term_exists( $term, 'mycustomtaxonomy' );
    
            if( $existent_term && $existent_term['term_id'] ) {
    
                $term_id = $existent_term['term_id'];
    
            } else {
    
        $term = wp_insert_term(
        $term, // the term
          'mycustomtaxonomy', // the taxonomy
          array(
            'description'=> 'This is '.$term.'!',
            'slug' => $term
          )
        );
    
        if( !is_wp_error($term ) && $term['term_id'] ) {
                     $term_id = $term['term_id'];
                } 
    
           }
        //Fill the array of terms for later use on wp_set_object_terms
           $terms[] = (int) $term_id;
    
        }
        wp_set_object_terms( $listing_id, $terms, 'mycustomtaxonomy' );
    } ?>

    Sorry, PHP is not my best spoken language;)
    Thanks

    Use the isset() check for the post variable like so:

    if ( isset( $_POST['checkbox1']  ) ) {
        // do  stuff
    }

    For the $input_terms variable check whether is empty like so:

    if ( !empty ( $input_terms ) ) {
       // do stuff
    }

    Thread Starter wzshop

    (@wzshop)

    thank you!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom field to custom taxonomy term?’ is closed to new replies.