Advanced Custom Fields Plugin: Get file URL into Parent

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:

Read More
    <?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.

Related posts

Leave a Reply

1 comment

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