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.

On parent page, I've got a grid of custom field values from child pages. All text fields work fine, but I'm having issues with the file URL fields: instead of the URL it returns attachment ID.

Here's the code I'm using to display all the fields:

    <?php
    //get children of page 4 and display with custom fields

    $args=array(
    'post_parent' => 4,
    'post_type' => 'page',
    );

    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {

    while ($my_query->have_posts()) : $my_query->the_post(); ?>

Stripped down version, without any formatting:

    <?php $meta_one = get_post_meta($post->ID, 'doors', true); ?>
    <?php echo $meta_one; ?>

ACF's developer said I should be using get_field instead, but if I replace get_post_meta with get_field it doesn't even return the ID.

I've also tried adding URL parameter like this: <?php echo $meta_one['url']; ?> No change.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Here's how ACF keeps attachment data:

meta_id        post_id     meta_key        meta_value

971            931         doors           666
972            931         _doors          field_25

As you can see if you're using get_post_meta it will only ever be able to return you an ID. Since not using get_field you'll need to do the extra step of getting attachment metadata yourself:

$meta_one = get_post_meta($post->ID, 'doors', true);
$meta_url = wp_get_attachment_url( $meta_one );
echo $meta_url;
share|improve this answer
    
Of course I've done that, it's set to URL and returns the URL on the child page. Only having issues with getting the URL to show on the parent page. –  Phantasmix Apr 3 '13 at 4:16
    
Well, how are you getting the other fields? Should be no different. Did you examine your $meta_one? Not echo, but rather print_r($meta_one). –  dalbaeb Apr 3 '13 at 5:38
    
The code I included in the original post is how I'm getting all the other fields into the parent. It works for text fields. -- print_r still prints the ID, not URL. –  Phantasmix Apr 3 '13 at 12:52
    
@Phantasmix I updated my answer, try it out. –  dalbaeb Apr 3 '13 at 15:31
    
Awesome, thank you!! That works great. I didn't even think to look in the db, duh :) Thanks –  Phantasmix Apr 4 '13 at 5:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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