• Resolved Samantha

    (@dydyt)


    Hi there,

    I can’t figure out how do not raplace existing post tags with a empty value.

    For eaxample: I imported posts with the following json file:

    [
       {
          "name":"Kialeas",
          "tags":"red, blue, green"
       },
       {
          "name":"Orbital",
          "tags":"one, two, three"
       },
       {
          "name":"Manique",
          "tags":"cat, dog, pig"
       }
    ]   

    Now i want to update with the following:

    [
       {
          "name":"Kialeas",
          "tags":"sky, moon, earth",
          "cat":"One"
       },
       {
          "name":"Orbital",
          "tags":"audi, bmw, merc",
          "cat":"Two"
       },
       {
          "name":"Manique",
          "tags":"",
          "cat":"Three"
       }
    ]  

    If "tags":"" is empty it will remove exiting post tags.

    How to avoid it?

    Thank you!

    • This topic was modified 5 years, 11 months ago by Samantha.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @dydyt

    To auto-detect that tags are missing and keep the existing ones on a per-record basis, you can write some custom code that uses our API: https://www.wpallimport.com/documentation/advanced/action-reference/#wp_all_import_set_post_terms.

    Here is an example snippet that you can modify for your use case (this shows how to keep the “Featured” category in-tact even if it’s not in the import record):

    /**
     * Example: Leave assigned category "Featured" in tact, even if it's not included in the import.
     *
     */
    
    add_filter( 'wp_all_import_set_post_terms', 'wpai_wp_all_import_set_post_terms', 10, 4 );
    
    function wpai_wp_all_import_set_post_terms( $term_taxonomy_ids, $tx_name, $pid, $import_id ) {
            if ( $tx_name == 'product_cat' ){
                    $txes_list = get_the_terms($pid, $tx_name);
                    if ( ! empty($txes_list) ){
                            foreach ($txes_list as $cat){
                                    if ($cat->name == 'Featured'){
                                            $term_taxonomy_ids[] = $cat->term_taxonomy_id;
                                            break;
                                    } 
                            }
                    }
            }
            return $term_taxonomy_ids;
    }
    Plugin Author WP All Import

    (@wpallimport)

    Hi @dydyt

    I’m marking this as resolved since we haven’t heard back. You can respond here if you still have questions about this.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Do not replace with empty element value’ is closed to new replies.