WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?
share|improve this question

closed as off-topic by Milo, ialocin, 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.

up vote 0 down vote accepted

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