Beispiel #1
0
 function save_post($post_id)
 {
     // bail ealry if no terms
     if (empty($this->save_post_terms)) {
         return;
     }
     // vars
     $info = acf_get_post_id_info($post_id);
     // loop
     foreach ($this->save_post_terms as $taxonomy => $term_ids) {
         // save
         wp_set_object_terms($info['id'], $term_ids, $taxonomy, false);
     }
     // reset array ( WP saves twice )
     $this->save_post_terms = array();
 }
Beispiel #2
0
function acf_delete_metadata($post_id = 0, $name = '', $hidden = false)
{
    // vars
    $return = false;
    $prefix = $hidden ? '_' : '';
    // get post_id info
    $info = acf_get_post_id_info($post_id);
    // bail early if no $post_id (acf_form - new_post)
    if (!$info['id']) {
        return $return;
    }
    // option
    if ($info['type'] === 'option') {
        $name = $prefix . $post_id . '_' . $name;
        $return = delete_option($name);
        // meta
    } else {
        $name = $prefix . $name;
        $return = delete_metadata($info['type'], $info['id'], $name);
    }
    // return
    return $return;
}
Beispiel #3
0
function get_field_objects($post_id = false, $format_value = true, $load_value = true)
{
    // global
    global $wpdb;
    // filter post_id
    $post_id = acf_get_valid_post_id($post_id);
    $info = acf_get_post_id_info($post_id);
    // vars
    $meta = array();
    $fields = array();
    // get field_names
    if ($info['type'] == 'post') {
        $meta = get_post_meta($info['id']);
    } elseif ($info['type'] == 'user') {
        $meta = get_user_meta($info['id']);
    } elseif ($info['type'] == 'comment') {
        $meta = get_comment_meta($info['id']);
    } elseif ($info['type'] == 'term') {
        $meta = get_term_meta($info['id']);
    } else {
        $rows = $wpdb->get_results($wpdb->prepare("SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $post_id . '_%', '_' . $post_id . '_%'), ARRAY_A);
        if (!empty($rows)) {
            foreach ($rows as $row) {
                // vars
                $name = $row['option_name'];
                $prefix = $post_id . '_';
                $_prefix = '_' . $prefix;
                // remove prefix from name
                if (strpos($name, $prefix) === 0) {
                    $name = substr($name, strlen($prefix));
                } elseif (strpos($name, $_prefix) === 0) {
                    $name = '_' . substr($name, strlen($_prefix));
                }
                $meta[$name][] = $row['option_value'];
            }
        }
    }
    // bail early if no meta
    if (empty($meta)) {
        return false;
    }
    // populate vars
    foreach ($meta as $k => $v) {
        // Hopefuly improve efficiency: bail early if $k does start with an '_'
        if ($k[0] === '_') {
            continue;
        }
        // does a field key exist for this value?
        if (!array_key_exists("_{$k}", $meta)) {
            continue;
        }
        // get field
        $field_key = $meta["_{$k}"][0];
        $field = acf_get_field($field_key);
        // bail early if no field, or if the field's name is different to $k
        // - solves problem where sub fields (and clone fields) are incorrectly allowed
        if (!$field || $field['name'] !== $k) {
            continue;
        }
        // load value
        if ($load_value) {
            $field['value'] = acf_get_value($post_id, $field);
        }
        // format value
        if ($format_value) {
            // get value for field
            $field['value'] = acf_format_value($field['value'], $post_id, $field);
        }
        // append to $value
        $fields[$field['name']] = $field;
    }
    // no value
    if (empty($fields)) {
        return false;
    }
    // return
    return $fields;
}