I feel like this is going to be one of those slap yourself on the head questions, but here it goes anyway:

I have a multi-dimensional array of data:

    [0] => Array
            [id] => 1033
            [zip] => 27604
            [distance] => 0

    [1] => Array
            [id] => 1024
            [zip] => 27615
            [distance] => 6


The id field in each is the post ID and what I'm really after. What I've tried thusfar is something like:

foreach( $locations as $post ) {

    echo get_the_title();


with no luck. Am I approaching this all wrong - what's the best way to get that darned ID value to run through setup_postdata()?

I still need to reference the other data in the multi-dimensional array (zip and distance).


There was a similar (related) question on here recently. The upshot is that get_the_title() uses the global $post when no argument is passed. And setup_postdata() doesn't set that global (a mistake I've made before now ).

In your case, I would do the following:

foreach( $locations as $post ) {

    echo get_the_title($post['id']);


Note: setup_postdata() takes a post object, not ID, so if you're going to use it, use it with get_post().

  • Appreciate the response - is there even an advantage to using setup_postdata() then based on how the results are formatted (since I have to pass in the $post['id'] var in this case)? – Zach Nov 19 '12 at 21:12
  • It's not needed for just the title - but investigating the source, setup_postdata() sets up the global $pages, which get_the_content() uses. So if you want to output content, then yes. I would use it anyway, to avoid any confusing bugs later down the road. – Stephen Harris Nov 19 '12 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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