Skip Navigation

[Resolved] Get ID from a media file

This support ticket is created 2 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 2 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#2219443

Hello,

I have made a custom post type with a file-field in backend. Instead of just showing the URL of the file-field in frontend, I want to show the ID of the attachment in addition to the url of the uploaded file.

How to achive this in frontend?

Best
Svein Magne

#2219493

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Svein,

Thank you for getting in touch.

Unfortunately this is not something that can be achieved by default with our Toolset plugins and as such you will need to make use of a custom shortcode to do this.

Add the following to your custom shortcodes section at Toolset -> Settings -> Custom Code and activate it.
<Code>

// Add Shortcode
function get_file_id( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'field' => '',
'post_id' => ''
),
$atts
);

$file_url = get_post_meta($atts['post_id'],$atts['field']);
$file_id = attachment_url_to_postid( $file_url );
return $file_id;

}
add_shortcode( 'get_file_id', 'get_file_id' );

[/php]

You can use the shortcode by doing the following [get_file_id field='wpcf-my-field' post_id='[wpv-post-id]']
Now replace wpcf-my-field with the slug of your custom field keeping the wpcf- prefix.

Please let me know if this helps.
Thanks,
Shane

#2221235

Shane,

Thanks for your support and attached code. Unfortunately the code you provided returned a 0, not the ID of the media file.

My custom field is named: pdf
[get_file_id field='wpcf-pdf' post_id='[wpv-post-id]']

When I use the shortcode [wpv-post-id] the post_id is returned correctly in the view.

Do you have any suggestions what is wrong with my code?

Best
Svein Magne

#2221457

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Svein,

I've updated the code a bit. This one should now work.

function get_file_id( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'field' => '',
'post_id' => ''
),
$atts
);

$file_url = get_post_meta($atts['post_id'],$atts['field']);
$file_id = attachment_url_to_postid( $file_url[0] );
return $file_id;

}
add_shortcode( 'get_file_id', 'get_file_id' );

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.