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); // vars $meta = array(); $fields = array(); // get field_names if (is_numeric($post_id)) { $meta = get_post_meta($post_id); } elseif (strpos($post_id, 'user_') !== false) { $user_id = (int) str_replace('user_', '', $post_id); $meta = get_user_meta($user_id); } elseif (strpos($post_id, 'comment_') !== false) { $comment_id = (int) str_replace('comment_', '', $post_id); $meta = get_comment_meta($comment_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) { $meta[$row['option_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 not a parent field if (!$field || acf_is_sub_field($field)) { 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; }
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); // vars $value = array(); // get field_names if (is_numeric($post_id)) { $keys = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d and meta_key LIKE %s AND meta_value LIKE %s", $post_id, '_%', 'field_%')); } elseif (strpos($post_id, 'user_') !== false) { $user_id = str_replace('user_', '', $post_id); $keys = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = %d and meta_key LIKE %s AND meta_value LIKE %s", $user_id, '_%', 'field_%')); } elseif (strpos($post_id, 'comment_') !== false) { $comment_id = str_replace('comment_', '', $post_id); $keys = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->commentmeta} WHERE user_id = %d and meta_key LIKE %s AND meta_value LIKE %s", $comment_id, '_%', 'field_%')); } else { $keys = $wpdb->get_col($wpdb->prepare("SELECT option_value FROM {$wpdb->options} WHERE option_name LIKE %s", '_' . $post_id . '_%')); } if (is_array($keys)) { foreach ($keys as $key) { // get field $field = get_field_object($key, $post_id, $format_value, $load_value); // validate field if (empty($field)) { continue; } // ignore sub fields if (acf_is_sub_field($field)) { continue; } // append to $value $value[$field['name']] = $field; } } // no value if (empty($value)) { return false; } // return return $value; }