How to get Advanced Custom Fields field key from WordPress database?

后端 未结 8 1612
名媛妹妹
名媛妹妹 2020-12-31 01:15

I’m using Advanced Custom Fields with post-type. I have some select custom fields, and I want to show all the label choices from each field.

I’ve tried this way.

相关标签:
8条回答
  • 2020-12-31 02:13

    Just trying to do this myself so I did some investigation. Seems each field and field group for ACF are stored in the wp_posts table in the database as custom post types. fields are 'acf-field' and groups are 'acf-field-group'.

    I was able to use this function to get the field key to then use update_field($field_key, $value) on posts that didn't have the field already.

    function get_acf_key($field_name){
        global $wpdb;
    
        return $wpdb->get_var("
            SELECT post_name
            FROM $wpdb->posts
            WHERE post_type='acf-field' AND post_excerpt='$field_name';
        ");
    }
    

    Then I was able to use:

    update_field(get_acf_key('my_field_name'), 'some value', $post_id);
    

    To either update the field for posts that had it already or add the field and it's key reference to posts that did not already have the field.

    0 讨论(0)
  • 2020-12-31 02:15

    ACF provides some easy ways to help keep multiple environments in sync - you can register your fields with PHP or a local JSON file. Doing this would allow you to keep using get_field_object() with a single field key across multiple environments. See:

    http://www.advancedcustomfields.com/resources/register-fields-via-php/

    http://www.advancedcustomfields.com/resources/local-json/

    I usually do my ACF development with the user interface and then export all of my fields groups as PHP to deploy across multiple environments.

    UPDATE with a simple example: You could add this to functions.php or a custom plugin to add your field to multiple environments programmatically .. then you call get_field_object() with a common field_key across all environments

    add_action('acf/init', 'wp123_add_local_acf_fields');
    function wp123_add_local_acf_fields() {
    
        acf_add_local_field_group(array(
            'key' => 'group_1',
            'title' => 'My Group',
            'fields' => array (
                array (
                    'key' => 'field_51ac9d333d704',
                    'label' => 'Color de pelo',
                    'name' => 'hair_color',
                    'type' => 'select',
                    'instructions' => 'Selecciona el color de pelo'
                    'required' => 0,
                    'choices' => array (
                        'bald' => 'Calvo',
                        'brown' => 'Castaño',
                        'brunette' => 'Moreno',
                        'red' => 'Pelirrojo',
                        'blonde' => 'Rubio',
                    ),
                    'default_value' => array (
                    ),
                    'allow_null' => 1,
                    'multiple' => 0,
                )
            ),
            'location' => array (
                array (
                    array (
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'post',
                    ),
                ),
            ),
        ));
    
    }
    
    0 讨论(0)
提交回复
热议问题