When reading posts with WP_Query how would I get the data type of an advanced custom field (textbox, date, number, repeater, …) ?

My code:

$loop = new WP_Query( $args );  
$posts = $loop->get_posts(); 
foreach ( $posts as $post ) {
    $custom_fields = get_post_custom($post->ID); 
    foreach ( $custom_fields as $field_key => $field_values ) {
        // Get type of field? How?

The first version of my answer was that in general you would have to code that information yourself into the meta key itself. For example:


But then I noticed the ACF tag of your question.

If you have a custom field key price, then it's ACF field key is saved to the hidden _price custom field. For example as field_4fea85f5320da.

Then you can use

get_field_object( $field_key, $post_id, $options )

to retrieve all the ACF information related to the custom field:

    [key] => field_4fea85f5320da
    [label] => Text Field
    [name] => text_field
    [type] => text
    [instructions] => 
    [required] => 0
    [default_value] => 
    [formatting] => html
    [order_no] => 0
    [value] => Use the option parameter to toggle loading the value

Check the ACF documentation on get_field_object here.

You could for example try to fetch it with:

$field_key = get_post_meta( '_price', TRUE );
if( ! empty( $field_key ) )
    $data = get_field_object( $field_key, $post_id );
    if( isset( $data['type'] ) )
        $type = $data['type'];
| improve this answer | |
  • Hi, this was almost a "copy into code window" solution. Thanks very much. Works a charm! – Matthias Max May 21 '14 at 20:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.