Home › Forums › General Issues › Only Display Custom Field Choices with Posts
I’m working on a page template that will display a simple unordered list of all the choices for a custom field called “family_name” (a Select field type). I’ve worked it to the point where it displays the FULL list of ALL choices, but now I want it to only display those choices that actually have posts. For example, if there aren’t any posts assigned to the ‘Smith’ family, then I don’t want ‘Smith’ to be displayed in my unordered list.
I feel like this should be super easy to do but I’m at my wit’s end.
Here’s what I’ve got so far:
<?php
$field_key = "field_569a742fe094f";
$field = get_field_object($field_key);
$families = $field['choices']; ?>
<ul>
<?php
if( $field ){
foreach( $families as $family ) {
echo '<li>' . $family . '</li>';
}
} ?>
</ul>
How can I modify this so it will only display those choices that actually have 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/
Thanks John. The field allows a single choice only.
Unfortunately, now it’s not displaying anything at all. I think I know why, but I’m not sure how to fix it. In the ACF admin screen where I set up the custom field, under the Choices section, the instructions say:
Enter each choice on a new line.
For more control, you may specify both a value and label like this:
red : Red
blue : Blue
Instead of setting up my choices the way the example shows where the value and the label are the same (red: Red), I set them up so they’re different:
1 : Red
2 : Blue
(Except of course in my case I have it set up for family names (1: Jones, 2: Smith, etc))
I noticed, though, that if I change things so that the choice values and the choice labels are the same, just like they are in the example (i.e. jones : Jones, smith : Smith), then the query we’re working on here works just fine. But if they’re different, then our query draws up an empty list as though no posts are tagged to any of the choices.
The reason I set up the choices with different values & labels is because it allows me to sort & order the choices numerically in other places on my site. It’s worked extremely well up until now & solved a number of other problems I’ve faced in the past. So if possible, I’d like to keep them that way.
Any suggestions?
Thinking out loud, I wonder if the reason our query draws a blank when the values & labels are different is because it’s searching for the choice values (“1” or “2”) when we need it to be searching for choice labels (“Smith” or “Jones”). The reason I say that is because when I hard-code in one of the values into our array, the query works just fine for that one value. But if I hard-code in one of the labels, then it doesn’t.
Is there a way to add further specification to this part of our query so it targets the choice value instead of the choice label?
array(
'key' => 'family_name',
'value' => $family
)
If it looks more specifically for $family->label instead of just $family, maybe that might solve the issue??? ($family->label doesn’t actually work of course; not sure what the syntax would be for that)
Change to foreach to
foreach( $families as $value => $family ) {
then change the meta query to
array(
'key' => 'family_name',
'value' => $value
)
The topic ‘Only Display Custom Field Choices with Posts’ 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.