Home Forums General Issues Get Category ID from ACF select menu and use as a variable


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 ( ), but i’m not seeing any answers looking at that page :/

    here’s my code:

     <?php wp_reset_query(); ?>
        		$myNumber = 0;
                $type = 'models';
    			$cat = the_field('list_category',''); 
                //echo $type;
                  //'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?)
    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


  • 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?

  • Thanks my issue has been fixed .

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Get Category ID from ACF select menu and use as a variable’ is closed to new replies.