Support

Account

Home Forums ACF PRO Enter values through the database

Solved

Enter values through the database

  • Hi!

    I have a problem, and maybe someone will be able to help me.

    I forgot to create a acf custom field initially, so I have a requirement to add it now and fill it with values. There are 450 posts.

    Instead of going through every post and add the data, my intention was to go to the phpmyadmin, select those posts and edit them manually, inline.

    But the problem is, that field was not created, that is, it will be obviousy created if I open the post and enter the data, and save it.

    So, my question is…Can I force the creation of that field for every old post (with default value set in ACF) so I can easily edit it in phpmyadmin?

    Any alternative solution would be welcome also.

    Thanks.

  • It would be very difficult to add fields directly through phpMyAdmin. Each field requires to entries in the database for each post. The first has a meta_key of your field name and the value for the field. More about values in a minute. The second field has a meta key that is equal to "_{$field_name}" and the value of this is the field_key of the ACF field.

    Values for other than the basic field types can be complicated. Some of them hold arrays. Some of them hold the ID value of another WP object. So the value you insert is highly dependent on what type of field you’re adding data for.

    I won’t even go into the extra complications of repeaters, flex and clone fields.

    Honestly, entering data directly into the db using phpMyAdmin won’t save you much, if any, time over updating each post manually.

    The best solution for doing something like this is WP All Import with the ACF add on http://www.wpallimport.com/ if you’re looking to save yourself some time. Even this would be questionable unless all of the fields you’re going to add have the same value and you can do a quick copy when creating a spreadsheet to be imported.

  • Thanks for your reply.

    To give some extra clarification.

    I have a field group for a custom post type.

    There are 8 custom fields and I added another one text field that has a default value, and I need to add sufix into that field for every post (4 charachters).

    My inital attempt was to find those fields in the database and edit them directly in the post_meta table. The problem is there are no entries (empty, with default value) created for me to be able to update them.

    Maybe they are somewhere else in the database? I assume they must exist because when I filter them in the facetwp there are all the fields with the default value set in the field group, but when queried in the database, there are none, except those that are updated through wordpress post.

    Any suggestion would be appreciated. Thanks.

  • You would need to insert all these default values.

    If there is a default value that will be used for each then there are some ways to deal with that.

    1) In your template where you get the value, check the value. This is a good practice anyway.

    
    $value = get_field('the_new_field');
    if (!$value) {
      $value = 'default value';
    }
    // do something with value
    

    2) Add a load value filter

    
    add_filter('acf/load_value', 'default_value_for_my_new_field', 10, 3);
    function default_value_for_my_new_field($value, $post_id, $field) {
      if (!$value) {
        $value = 'my_default_value';
      }
      return $value;
    }
    

    In either case, the value can be adjusted to be different based on the post that it is used for with a bit of coding.

    If you want to insert the values into the database then you must insert 2 values for every post. 1) the field value 2) The acf field key reference. The best thing for you to do is to update at least one post with a value and then look in the database and use this as an example to see what you need to insert for each of the other posts.

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

You must be logged in to reply to this topic.