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

后端 未结 8 1675
名媛妹妹
名媛妹妹 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: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);
    }
    

提交回复
热议问题