Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 -

    <?php 
    $the_args = array (
        'post_type' => 'jobs',
        the_field('job_category') 
    );

    $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; ?>
        </select>
    </form>

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

<?php 
    $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') ?> 
        </div>
        <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!

Alex.

share|improve this question

1 Answer 1

up vote 0 down vote accepted
$the_args = array (
    'post_type' => 'jobs',
    the_field('job_category') 
);

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 ?>
    </select>
<?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

 
discard

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.