Home › Forums › General Issues › Only Display Custom Field Choices with Posts › Reply To: Only Display Custom Field Choices with Posts
It will be different depending on if the select field on the post allows a single choice or multiple choices.
With a custom field you’re going to need to do a WP_Query for every name in the loop. http://codex.wordpress.org/Class_Reference/WP_Query
// select single
// all of this goes inside your foreach loop
$args = array(
'post_type' => 'post',
'posts_per_page' => 1, // only need 1, just checking for any posts with the value
'meta_query' => array(
array(
'key' => 'family_name'
'value' => $family
)
)
)
$query = new WP_Query($args);
if (count($query->posts)) {
echo '<li>' . $family . '</li>';
)
// multiple choices allowed
// all of this goes inside your foreach loop
$args = array(
'post_type' => 'post',
'posts_per_page' => 1, // only need 1, just checking for any posts with the value
'meta_query' => array(
array(
'key' => 'family_name'
'value' => '"'.$family.'"',
'compare => 'LIKE'
)
)
)
$query = new WP_Query($args);
if (count($query->posts)) {
echo '<li>' . $family . '</li>';
)
Either way, if there are a lot of choices then this is going to be extremely slow. You would be better off in the long run to use a custom taxonomy, something that’s built right into WP, then you just need to use get_terms() with the right arguments https://developer.wordpress.org/reference/functions/get_terms/
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.