1

I have a relationship acf field that displays posts from a custom post type that are then displayed on the front end.

The post type is publications and I want to link to the publications uploaded file as opposed to the single page link, how would I do this?

Here is the code for the relationship field I have:

$posts = get_sub_field('download_existing');
     $existing_link_url = '';
     foreach ((array)$posts as $link_posts) {
        $existing_link_url = get_permalink($link_posts->ID);
}

I know I am getting the posts permalink in the for each loop but I have tried wp_get_attachment_url and it doesn't seem to link through the file.

Is it possible to do what I am trying to do, I have looked on the ACF website and searched for this but I can't find anything.

  • Where is the file uploaded? Do you upload to the content of the post? Not sure what the value of $posts is. Just an Array of WP_post class? – Drupalizeme Nov 2 '17 at 15:19
  • The file is uploaded to an acf field not the content, $post is an array of the WP_Post class, yes @Drupalizeme – Neelam Khan Nov 2 '17 at 15:21
1
$posts = get_sub_field('download_existing');
     $existing_link_url = '';
     foreach ((array)$posts as $link_posts) {
       $existing_link_url = get_field("your_field",$link_posts->ID);
     }

This will overite in the loop the $existing_link_url so you may want to save it.

  • I have literally just done this, it only came to me after your first comment and then you added the same answer, thanks for your help! – Neelam Khan Nov 2 '17 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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