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.

So I'm trying to populate an Advanced Custom Fields selectbox with a nice complicated array based off some post data. I've been trying to do this by doing a custom query and loop through the returned posts.

Unfortunately, the loop seems to return diddly squat. On my test install, it should be returning 10 posts. However, the var_dump in the below statement will return NULL 10 times. Setting get_post($post) inside the loop will return the first result for every instance.

Weirdly, var_dumping $hw_selectbox_query will return a seemingly correct object, so it's definitely an issue with the actual loop itself not defining the $post object. I could run a foreach loop through the returned query, but that doesn't feel like the right way to do it.

FWIW, the function runs perfectly when placed on a blank front-end page.

function populate_selectbox_field( $field ){

    $field['choices'] = array();

    $hw_selectbox_args = array(
        post_type => 'custom_post_type',
        orderby => 'title',
        order => 'ASC',
        posts_per_page => -1
    );

    $hw_selectbox_query = new WP_Query($hw_selectbox_args);

    if ( $hw_selectbox_query->have_posts() ) : while ( $hw_selectbox_query->have_posts() ) : $hw_selectbox_query->the_post();

        var_dump($post);

        $field['choices'][$post->ID] = $post->post_title

        // Do more complicated stuff in real life

    endwhile; endif;

    return $field;
}

add_filter('acf/load_field/name=destination_node', 'populate_destination_node_field');
share|improve this question
    
The location of your var_dump is outside your custom loop, and therefore, has nothing to do with your custom loop. Move it just after your comment \\ Do more complicated stuff (but before the endwhile;) and let us know what you get. –  cale_b Oct 17 '13 at 19:44
    
Sorry, well spotted. Unfortunately, I'd just accidentally put it there when composing the question. It does output NULL when it's actually inside the loop. And probably outside it too. –  Magnakai Oct 17 '13 at 19:50
    
And what if you put the_title(); before / after the var_dump? –  cale_b Oct 17 '13 at 19:51
    
See, that works. Why can't I get at the $post object within the loop? I need various bits from it to build my array. I've had a look at the get_the_title() source code, but can't quite see what's different. core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/… –  Magnakai Oct 17 '13 at 19:54
1  
Realized what I was missing, built an answer for you. Let me know if it works properly. –  cale_b Oct 17 '13 at 19:56

1 Answer 1

up vote 1 down vote accepted

When using a custom query, in order to access the $post information, you need to reference post as a class variable the custom WP_Query.

Since you constructed the query as:
$hw_selectbox_query = new WP_Query($hw_selectbox_args);

Then you would access the post variable of $hw_selectbox_query, like so:

var_dump($hw_selectbox_query->post);

This will give you the information you need / want to build your $field array.

share|improve this answer
    
Oh. my. god. I'm an idiot. Thanks so much! That works perfectly. I've done this plenty of times before, and yet I just spent an hour missing something this obvious. Thanks for unmuddling my head! –  Magnakai Oct 17 '13 at 20:01
    
No worries. We all need a second pair of eyes sometimes! –  cale_b Oct 17 '13 at 20:09

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.