Home › Forums › General Issues › Display a list of media in custom post type
Hi,
I used CPT UI to create a custom post type. Then, I want to display on these custom posts a list of media (mainly pictures).
How can I display a field on media meta where I chose the custom posts where the media should be displayed ? Which field type should I use ?
Thanks,
David
Hi @sireagroup
I suggest you use the post object field and set the location rule to “Attachment <=> is equal to <=> All”.
I hope this helps 🙂
Hi,
Thank you for your answer.
This is what I tried before posting, but this way doesn’t retrieve anything. When I debug, this is empty …
Hi @sireagroup
Could you please share the code you used to debug it? If you need to get the value from an attachment, please check this page out: https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-media-attachment/.
If you want, you can also use a gallery field on your custom post type so you can set which images you want to show up on that post.
Thanks 🙂
I paste this :
<?php echo '<pre>';
print_r( get_field('media-test') );
echo '</pre>'; ?>
“media-test” is the name of the field displayed on a media in the my media library in the admin panel.
I selected a custom post displayed in the field, but when I use the code from post object in the PHP of my custom post single template, nothing is retrieved.
The gallery field is a good way, I will use it if the first option doesn’t work.
Hi @sireagroup
As stated in the link I gave you before, you need to pass the ID of the media as the second parameter of the get_field() function like this:
<?php
echo '<pre>';
print_r( get_field('media-test', 99) );
echo '</pre>';
?>
Where “99” is the ID of your media. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-media-attachment/.
I hope this helps 🙂
Finally I solved it !
Here is what I used, it works : https://www.advancedcustomfields.com/resources/querying-relationship-fields/.
but I have another problem :
The number of elements used in reverse query are medias, but I don’t know why i can only display 5 medias, no more. Does someone know why ?
Hi @sireagroup
I believe that’s because you have an option on your site that allows only five posts. In this case, you need to use the posts_per_page
option like this:
$doctors = get_posts(array(
'post_type' => 'doctor',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'location', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
Hope this helps 🙂
The topic ‘Display a list of media in custom post type’ 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.