Home › Forums › General Issues › Get Category ID from ACF select menu and use as a variable
hello,
i’m trying to let the user select a category that will be used by the page template to display a list of posts in that category. I would like to let the user select a category, and use the Category ID to use as a variable in a wp_query.
i think it’s got something to do with this ( http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/ ), but i’m not seeing any answers looking at that page :/
here’s my code:
<?php wp_reset_query(); ?>
<?php
$myNumber = 0;
$type = 'models';
$cat = the_field('list_category','');
//echo $type;
$args=array(
//'post_type' => $type,
'cat' => $cat,
'post_status' => 'publish',
'posts_per_page' => -1,
'post__not_in' => array($post->ID),
'caller_get_posts'=> 1 );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
ACF settings are:
name: list_category
type: taxonomy (is this right?)
taxonomy:categories
field type: check box ( i would have thought a drop menu would make more sense but dont see that option )
return value: term ID
this works except for the fact that it also displays the category number on the page, because it’s a string still i guess.
any help would be appreciated!
Hi @colin
There are a few issues with your above code.
1. Misuse of the the_field function. This should be get_field.
2. The taxonomy field does have a drop-down (single select) mode
3. the param ‘cat’ expects an integer. If you selected checkbox for the field type, you will have an array of ids, not just one.
I would advise you to change the taxonomy field type to a single select, and then fix up the code from point 1.
Good luck
Cheers
E
Hi Elliot,
Thanks for taking the time to help.
1. i did have that, but tried the_ for some reason. i blame 2 and 3
2. aah HA. didn’t realize that was what that meant. it’s crazy obvious now! >.<
3. so, i could have maybe grabbed the first value using $cat[0]; ?
Thanks again – where’s the ‘buy me a pint’ button?
The topic ‘Get Category ID from ACF select menu and use as a variable’ 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.