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?

closed as off-topic by Milo, Nicolai, s_ha_dum May 13 '14 at 16:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.


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'];
  • 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.