Support

Account

Home Forums General Issues How to import (wp all import) the custom fields attached to each term using ACF?

Solving

How to import (wp all import) the custom fields attached to each term using ACF?

  • Hello Gurus
    I want to import the custom fields attached to each term using ACF.
    Can you guys please help me?
    I did the my own code but it’s not working.
    Actually update_field() function seems not working for taxonomie’s term.
    I have used in this way update_field($key,$value,$term->taxonomy.’_’.$term->term_id);
    But still it’s not working.
    Please help me.
    Thanks.

  • Hi @sunilkushwaha01

    I’ve just tested the update_field() function and it was working correctly.

    Could you please make sure that the variables you used have the correct value? Also, I suggest you use the field key instead of the field name.

    Could you also try to update the field by providing the parameters manually like this:

    update_field('field_1234567890abc', 'test', 'taxonomy-slug_99');

    Thanks πŸ™‚

  • Hi @james ,

    Thanks for reply, actually the issue was the “field name” . I tried ‘field key’ and it’s worked for me yesterday. But I am trying to import the the CSV file (with the help of WP all Import plugin) having lots of ACF’s fields .
    Inside the loop of CSV data I am getting ACF field’s name but not it’s key. So for now I manually assigned the field key with if else condition. But this is not a good way I guess.

    I also tried to find the “field key” by it’s name by using the get_field_objects() but can’t find any luck.

    So can you please suggest me a better solution for this ?

    Thanks

  • Hi @sunilkushwaha01

    Could you please share an example of the data from the CSV file? ACF saves two entries in the database, the field value:

    field_name : value

    And the reference:

    _field_name : field_123456789abc

    The reference is needed by ACF to get and update the data from and to the correct field. That’s because you can have multiple fields with the same name, but not the same key. Could you please make sure that the reference is being imported too?

    If you still need to use the update_field() function, you can get the list of field keys like this:

    $fields_list = array();
    
    $fields_posts = get_posts(array(
        'posts_per_page' => -1,
        'post_type' => 'acf-field',
    ));
    
    foreach($fields_posts as $fields_post){
        $fields_list[$fields_post->post_excerpt] = $fields_post->post_name;
    }
    
    print_r($fields_list);

    I hope this helps πŸ™‚

  • Hi @james,

    I got this “ACF saves two entries in the database, the field value and another is reference”
    But the thing is I am using it for Taxonomy’s term, So I guess your given code won’t work in my case.
    Well after using the update_field function in this format
    update_field('field_1234567890abc', 'test', 'taxonomy-slug_99');
    it’s work for me. Now it’s storing the value and reference into WP_option table and I can access the value by ACF functions.

    Noe the only issue is how can I get “field key” for my each column. Please have a look at attached file. The column name is the same as ACF filed.

    So I guess I have to put the “field key” as column name instead of “filed name” what you say ?

    Thanks.

  • Hi @sunilkushwaha01

    Have you tried the code I gave you before? It will create an array of your the field name and the field key like this:

    Array
    (
        [checkbox] => field_56d988f557b3e
        [clone] => field_57b00d4d407ff
        [email] => field_56d986a0eb917
        [number] => field_56d9866e09dca
        [text_area] => field_56d986186d8ae
        [text] => field_56d985c1ebeb2
    )

    Then you can just get the field key like this:

    $field_name = 'text';
    $field_key = $fields_list[$field_name];

    But if you could use the field key in the CSV file, that would be great! Just make sure that it won’t confuse you when you need to check the file.

    Hope this helps πŸ™‚

  • Hi James,
    thank you so much for helping me,

    Ya tried your given this code

    $fields_list = array();
    
    $fields_posts = get_posts(array(
        'posts_per_page' => -1,
        'post_type' => 'acf-field',
    ));
    
    foreach($fields_posts as $fields_post){
        $fields_list[$fields_post->post_excerpt] = $fields_post->post_name;
    }
    
    print_r($fields_list);

    but it’s return the null array.
    So checked into database and there is no wp_post available having post_type ‘acf-field’.

    So still didn’t find any solution.

    Any other suggestion ?

    Thanks

  • Hi @sunilkushwaha01

    Are you using ACF free version? If you are, then you should be able to do it like this:

    $fields_list = array();
    $fields_posts = array();
    
    $group_posts = get_posts(array(
        'posts_per_page' => -1,
        'post_type' => 'acf',
    ));
    
    foreach($group_posts as $group_post){
        $fields = array();
        $fields = apply_filters('acf/field_group/get_fields', $fields, $group_post->ID);
        $fields_posts = array_merge($fields_posts, $fields);
    }
    
    foreach($fields_posts as $fields_post){
        $fields_list[$fields_post['name']] = $fields_post['key'];
    }
    
    print_r($fields_list);

    Could you please test it?

    Thanks πŸ™‚

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.