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 ?
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.
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.
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 ?
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 🙂
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!
© 2022 Advanced Custom Fields.