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

后端 未结 8 1611
名媛妹妹
名媛妹妹 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 01:50

    I'm throwing another option into the mix. I think the existing answers are good, but unless you look at the parent group, you will never get a reliable field key because the field name can exist across multiple groups.

    For example, lets say you have two custom groups - one for post type books, and one for custom post type movies. Both groups have added a field called title.

    In the database, both are stored with post_except = 'title' and post_type = 'acf-field'. Two entries with the same post_except, so any query relying only on post_except will be wrong, wildcard or not.

    Any query relying on post id is not great either, as a post might not always exist to pass in.

    So you need to pass in a combination of field and group to get the field key from the name. This snippet works well for me:

    if (! function_exists('acf_field_from_name')) {
    
        function acf_field_from_name($field, $group)
        {
            global $wpdb;
    
            return $wpdb->get_var($wpdb->prepare("
                SELECT post.post_name as field_name
                FROM $wpdb->posts AS post
                LEFT JOIN $wpdb->posts AS parent
                    ON post.post_parent = parent.id
                WHERE post.post_excerpt = %s
                    AND post.post_type = 'acf-field'
                    AND parent.post_excerpt = %s
                    AND parent.post_type = 'acf-field-group'
            ", $field, $group));
        }
    }
    

    Will return the field key from name and group, or null if none exists.

    Usage:

    acf_field_from_name('title', 'movie-fields'); // returns field_3333333333333
    
    acf_field_from_name('title', 'book-fields'); // returns field_4444444444444
    
    acf_field_from_name('plumbus', 'movie'); // returns null
    
    0 讨论(0)
  • 2020-12-31 01:50

    Had same problem, also ended up using key which lead me just to another dead end with no normal way to get key value, but luckily encountered this.

    Taken from - https://wordpress.stackexchange.com/questions/248006/acf-add-fields-values-to-newly-inserted-post

    function acf_getValue($fieldname, $post_id){
        if(($value = get_field($fieldname, $post_id)))
            return $value;
        $value = get_post_meta($post_id, $fieldname);
        return $value[0];
    }
    function acf_updateValue($fieldname, $value, $post_id){
        $field_key = 'field_' . uniqid();
        update_post_meta($post_id, $fieldname, $value);
        update_post_meta($post_id, '_'.$fieldname, $field_key);
        update_field($field_key, $value, $post_id);
    }
    

    Where acf_updateValue simulates how its done by ACF itself when you manually save. Since only update_field is not enough for ACF columns

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

    The way ACF works you really should use the key.

    from (http://www.advancedcustomfields.com/resources/get_field_object/)

    "You can and should use the $field_key 100% of the time.

    The problem with using $field_name is that if the reference does not already exist, ACF will not be able to find the field object and will not be able to save the value. This situation would occur if you had used code to insert a post.

    Also, it is more efficient to use the field_key as the first parameter in the update_field function as it bypasses the reference look up."

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

    The right way is to use acf_maybe_get_field function, just like that:

    acf_maybe_get_field( 'field_name', false, false );

    The arguments are: field name, post id (defaults to current post) and the most important strict which defaults to true, but we set it to false here to get field object even when it does not yet exist for the post.

    Refers to Get ACF field key programmatically by field name

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

    Here is a modified version of answer provided by @BFDatabaseAdmin matching the exact meta_value in "LIKE"

    function get_acf_key($field_name) {
      global $wpdb;
      $length = strlen($field_name);
      $sql = "
        SELECT `meta_key`
        FROM {$wpdb->postmeta}
        WHERE `meta_key` LIKE 'field_%' AND `meta_value` LIKE '%\"name\";s:$length:\"$field_name\";%';
        ";
      return $wpdb->get_var($sql);
    }
    
    0 讨论(0)
  • 2020-12-31 02:11

    There's no way to reliably retrieve the key using the name, because the name is metadata, and hence open to change, whereas the key (at least without editing the database manually) isn't.

    However, this function will work with the most recent version of ACF, with a couple of caveats. ACF creates field groups within posts as a custom post type of ACF, but all the data about the fields themselves is held within the post_meta table.

    function get_acf_key($field_name) {
    
        global $wpdb;
    
        return $wpdb->get_var("
            SELECT `meta_key`
            FROM $wpdb->postmeta
            WHERE `meta_key` LIKE 'field_%' AND `meta_value` LIKE '%$field_name%';
        ");
    
    }
    

    A warning: because the field name is stored as part of an array, when finding it via SQL you have to rely on a wildcard search, which is open to errors. As long as all your fields have entirely different names, you're fine, but if for example you have one field called "farm" and another called "farmer", this will error because it will find both fields.

    The only reliable way to update fields is to manually code the key, though this is admittedly clunky, which is what led me here to begin with.

    0 讨论(0)
提交回复
热议问题