Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 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.

1 Answer 1

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:

booking_date
booking_text
booking_number

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:

Array
(
    [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 at 20:46

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