I have an image type custom field in one of my field groups. I want to create a static page that has a list of all images entered into that custom field from all posts. I’m not well versed in php. I’ve read the documentation but can’t seem to find a solution. Any suggestions? I am using a custom post type if that helps.
you can achieve this with simple WP’s query.
One thing to keep in mind, acf’s value are saved in the “wp_postmeta” table. So, your code would be similar to these lines:
$args = array (
'post_type' => 'post', // your post type
'posts_per_page' => -1, // grab all the posts
'meta_key' => 'your_acf_image_field_name',
'meta_compare' => 'EXISTS' // make sure the post have this acf value
);
$query = new WP_Query($args);
while ($query->have_posts()): $query->the_post();
// because the image value is saved as attachment_id
echo wp_get_attachment_image( get_field('your_acf_image_field_name') );
endwhile;
wp_reset_query();
Thank you. I tried this and it’s not pulling in anything. I logged into the database to verify that my last couple posts were in fact going in with the correct post type and field name. Not sure how to troubleshoot this. Here’s what I have now:
<?php
$args = array (
'post_type' => 'all_characters', // your post type
'posts_per_page' => -1, // grab all the posts
'meta_key' => 'character_image',
'meta_compare' => 'EXISTS' // make sure the post have this acf value
);
$query = new WP_Query($args);
while ($query->have_posts()): $query->the_post();
// because the image value is saved as attachment_id
echo wp_get_attachment_image( get_field('character_image') );
endwhile;
wp_reset_query();
?>
can you try this so we can narrow things down.
while ($query->have_posts()): $query->the_post();
// because the image value is saved as attachment_id
$imgElement = get_field('character_image', get_the_ID());
$imgSrc = wp_get_attachment_image_src($imgElement, 'full');
echo "the image - ".$imgElement."<br>";
echo "the full size image src - ".$imgSrc;
endwhile;
if that fails, try changing the args array to use meta_query instead of meta_value
$args = array (
'post_type' => 'all_characters', // your post type
'posts_per_page' => -1, // grab all the posts
'meta_query' => array(
array('key' => 'character_image',
'compare' => 'EXISTS'
)
)
);
So, the $imgElement pulls in the image urls, but $imgSrc does nothing.
so you can simply do
echo ““;
instead of wp_get_attachment_image_src or wp_get_attachment_image
you dont need to get the attachment via the function call.
check what your acf image field is set to in the group as a return value, there are options like ‘object’, ‘id’ etc, what do you have it set to. I believe you have it set to url.
If you set it to id, i believe the previous code will work.
oops, code wrap
so you can simply do
echo <img src='".$imgElement."' class='img-responsive'>";
instead of wp_get_attachment_image_src or wp_get_attachment_image
you dont need to get the attachment via the function call.
check what your acf image field is set to in the group as a return value, there are options like ‘object’, ‘id’ etc, what do you have it set to. I believe you have it set to url.
If you set it to id, i believe the previous code will work.
The topic ‘List all entries from a custom field’ is closed to new replies.
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.