0

I have a standard Wordpress loop displaying items:

     $loop = new WP_Query( array( 
            'post_type' => 'images',   
            'orderby' => 'menu_order',
             ) );

   while ( $loop->have_posts() ) : $loop->the_post(); 

   /* loop code, HTML mostly, a few if statements, nothing special */

   endwhile;

Now I want to display CERTAIN data from meta boxes and here comes the trouble.

the_meta() works just fine, but get_post_meta() doesn't work at all.

Why is this happening? Any fixes? I need to do something like get_post_meta($post->ID, 'metabox_field', true).

Any ideas?

[update]

Alright. More details.

I have a custom post type called "images".

Each image item has it's own metabox with a few fields inside, let's call them image_date, image_author, copyrights.

I want to display image author from field called image_author, so:

<?php 
    $author = get_post_meta($post->ID, 'image_author', true); 
    echo $author;
?>

The problem is it works ONLY when I open SINGLE image (single-image.php), but doesn't in loop (and I want to display this data for each item in loop also).

The_meta() works in both places if it changes something.

My loop looks exactly as above.

I guess that's all :)

7
  • echo get_post_meta($post->ID, 'metabox_field', true); ? :) Commented Mar 30, 2011 at 17:34
  • I believe this works only inside items/pots but not with loops. At least not for me... :( Commented Mar 30, 2011 at 17:49
  • Doesn't work in what capacity? Would help a great deal if you could provide details of what you tried, what you expected to happen, what actually happened, etc.. "doesn't work" isn't really helpful in understanding the issue.
    – t31os
    Commented Mar 30, 2011 at 17:58
  • @t31os, sorry, take a look at [update]. Commented Mar 30, 2011 at 18:10
  • 1
    @WordPressor - What file(s) are you adding this loop code into? And also what URLs are you requesting when you expect to see these results? Additionally does every one of these posts have this meta field(definately)?
    – t31os
    Commented Mar 30, 2011 at 18:14

1 Answer 1

0

get_post_meta() function works In and Outside of the loop and you saying that you do see it working on a single image "but doesn't in loop" means that you probably did not add the code to the right loop.

To be clear It works with all posts types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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