Home › Forums › General Issues › get_field() not returning values on specific post ID for image
Newbie, here. I have added ACF fields to images in WP Media library. I believe that I’ve set up the fields correctly (in a field group) and connected the group to all attachments. When I go into the Media library, I see the fields and am able to edit them in wp admin. However, I am getting nothing when I do get_field in my PHP code (in functions.php). See code below.
I’m referring to the post ID directly to assure context. Also, there will be several images on a single page, so I want to identify the one I’m talking about specifically.
Any ideas?
function artwork_shortcode($atts) {
// Extract the attributes
$atts = shortcode_atts(
array(
'src' => '', // Default image URL
),
$atts,
'artwork'
);
// Your PHP code here
$image_url = esc_url($atts['src']); // get the image URL from the shortcode attribute
$post_id = attachment_url_to_postid($image_url); // get the image post ID from the image URL using the WordPress function
$artfile = get_field('art-media-file', $post_id); // get value of a flag that this is art file media using the post ID
$title = trim(get_field('art-title', $post_id)); // get title using the post ID
$title = trim($title,'"'); // remove quotes
$year = trim(get_field('art-year', $post_id)); // get year using the post ID
$dimensions = trim(get_field('art-dimensions', $post_id)); // get dimensions using the post ID
$medium = trim(get_field('art-medium', $post_id)); // get medium using the post ID
$availability = get_field('art-availability', $post_id); // get availability using the post ID
$output = '';
if( $artfile && !empty($title) ) { // if it's an art file with a title
$output .= '<div class="art-details-card">';
$output .= '<h2><em><strong>' . $title . ',</strong></em>';
if (!empty($year)) {
$output .= ' ' . $year;
}
$output .= '</h2>';
if (!empty($medium)) {
$output .= '<p>' . $medium . '</p>';
}
if (!empty($dimensions)) {
$output .= '<p>' . $dimensions . '</p>';
}
if ($availability ) { // if it's available
$output .= '<p><strong>AVAILABLE</strong></p>';
}
$output .= '</div>';
}
// Format the HTML code for the pattern
$debug_msg = '';
$debug_msg = '<!-- Vars: image_url = ' . $image_url . ', post_id = ' . $post_id . ', artfile = ' . $artfile . ', title = ' . $title . ', year = ' . $year . ', dimensions = ' . $dimensions . ', medium = ' . $medium . ', availability = ' . $availability . ' -->';
$html_out = $debug_msg . '
<div class="art-container" style="position: relative;">
<img src="' . $image_url . '" alt="Artwork image" />
'. $output . '</div>';
return $html_out;
}
endif;
add_shortcode('artwork', 'artwork_shortcode');
Are you sure that this is returning the correct post ID?
$post_id = attachment_url_to_postid($image_url);
I solved this by first getting the field group and then individually addressing each one:
// Extract the attributes
$atts = shortcode_atts( array( 'src' => '', ), $atts, 'artwork');
// Your PHP code here
$image_url = esc_attr($atts['src']); // sanitize the image URL from the shortcode attribute
$post_id = attachment_url_to_postid($image_url); // get the image post ID from the image URL using the WordPress function
$artwork = get_field('artwork', $post_id); // get artwork field group
$artfile = $artwork['artfile']; // get value of a flag that this is art file media
$title = trim($artwork['art-title']); // get title
$title = trim($title,'"'); // remove quotes
$year = trim($artwork['art-year']); // get year completed
$dimensions = trim($artwork['art-dimensions']); // get dimensions
$medium = trim($artwork['art-medium']); // get medium
$availability = $artwork['art-availability']; // get availability
I’m still having trouble with true-false values. In the above example, there are 2 true-false switches (checkboxes):
$artfile = $artwork[‘artfile’];
$availability = $artwork[‘art-availability’];
From what I read in the documentation, these should be returning 1 for true or 0 for false. However, they are returning empty, regardless of being checked or not. Any ideas?
$post_id = attachment_url_to_postid($image_url);
This code is returning the correct post ID, by the way. I am able to retrieve the text values correctly using get_field on the field group, and then grabbing the array values by field name; except for the true-false fields.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.