WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to populate a select box to get the categories created in my ACF post type, when clicked these will filter available jobs via their category. The post type can be seen here - Post Type

Currently my query returns the 4 categories to the select box which correspond with the posts rendered later on in my code, rather than rendering all of my categories.

My category query can be seen here -

    $the_args = array (
        'post_type' => 'jobs',

    $option_query = new WP_Query( $the_args );

    <form action="">
        <select name="job_cats">
            <?php if (have_posts()) : while ( $option_query->have_posts()) : $option_query->the_post(); ?>
                <option value="<?php the_field('job_category'); ?>"><?php the_field('job_category'); ?></option>
            <?php endwhile; endif; ?>

    <?php wp_reset_query(); ?>

I tried to add the reset_query function before running this function below, but the same results persist to render to the select box.

    $cat_filter = '';
    $node_id = 0;
    $node_count = 0;

    $args = array(
        'post_type' => 'jobs',
        'meta_key' => 'job_category',
        'meta_value' => $cat_filter

    $the_query = new WP_Query( $args );
    <?php if (have_posts()) : while ( $the_query->have_posts()) : $the_query->the_post(); $node_id++ ?>
        <div class="search-node node<?php echo $node_id ?>"> 
             <?php get_template_part( 'post' , 'jobs') ?> 
        <hr id="divider<?php echo $node_id ?>" class="search-divider">
    <?php endwhile; else: ?>
          <p class="none-found">Sorry, we currently have no jobs available, keep watching this space!</p>
    <?php endif; ?>

Any pointers would be appreciated, I am relatively new to wordpress so I am sure I have made a small mistake!


share|improve this question
up vote 0 down vote accepted
$the_args = array (
    'post_type' => 'jobs',

The trouble here is that the_field( 'job_category' ) prints data, so using it to build an array is pretty much useless. You're left with literally post_type = job.

However, the real problem is that you actually need to grab the category options, not the posts that you attach them to. That's why, when you run your loop, you just end up with a select that corresponds to each post's category.

A quick gander at the ACF documentation reveals...

$field = get_field_object('field_name');
$value = get_field('field_name');
$label = $field['choices'][ $value ];

This gives us a pretty clear idea of how to interact with data stored by ACF. $field['choices'] appears to be an associative array of the choices (categories), the key being the "field" & the value being the "label". With this info, let's have a crack at building that select:

<?php if ( $field = get_field_object( 'job_category' ) ) : ?>
    <select name="job_cats">
        <?php foreach ( $field['choices'] as $name => $label ) ?>
            <option value="<?php echo $name ?>"><?php echo esc_html( $label ) ?></option>
        <?php endforeach ?>
<?php endif ?>

Hope this helps, and let us know if you're unsure of anything, or if it doesn't work! :)

share|improve this answer
Brilliant this solved the problem! Thanks a lot, I feel silly when I can't work simple things like this out, makes a lot of sense now looking at your code :). – Alex Sims Jul 8 '13 at 11:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.