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'm trying to get all posts from a particular custom post type using the following code:

$auctions = get_posts(array('post_type' => 'auction'));
print_r($auctions);
echo '<select>';
foreach ($auctions as $auction) {
    echo '<option value="' . $auction->ID . '">' . $auction->post_title . '</option>';
}
echo '</select>';

Although the print_r() call shows data, the foreach seems to ignore it and doesn't print anything. Any ideas?

Any help would be appreciated

print_r() output:

Array (
    [0] => WP_Post Object (
        [ID] => 36
        [post_author] => 1
        [post_date] =>    2013-05-19 10:58:45
        [post_date_gmt] => 2013-05-19 08:58:45
        [post_content] =>
        [post_title] => My Title
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] =>
        [post_name] => my-title
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2013-05-24 09:55:53
        [post_modified_gmt] => 2013-05-24 07:55:53
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http://domain.com/?post_type=auction&p=36
        [menu_order] => 0
        [post_type] => auction
        [post_mime_type] =>
        [comment_count] => 0
        [filter] => raw
    )   
) 
share|improve this question
    
What does the print_r( $auctions ); output? (Add it to your question; it'll be very hard to read in the comments.) –  Pat J May 28 '13 at 20:38
    
print_r() data added –  user1991185 May 28 '13 at 20:45
    
Does the <select>...</select> statement show up in the generated HTML? If you view the source of your page, does it look correct? –  Pat J May 28 '13 at 20:51
    
Oops... a typo I made prevented the correct output. Thanks anyway! –  user1991185 May 28 '13 at 21:05
    
@user1991185 is that typo had been the issue and question is resolved? –  Rarst May 28 '13 at 21:49
show 1 more comment

2 Answers

Perhaps because get_posts returns an object, you need to setup post data per Codex get_posts. Replacing line 4:

foreach($auctions as $auction) : setup_postdata($auction) { 
share|improve this answer
1  
get_posts() returns an array codex.wordpress.org/Template_Tags/get_posts#Return_Value, running setup_postdata would be used to allow the use template tags, like the_ID() –  Andrew Bartel May 28 '13 at 21:02
    
Ah, yes, I see that further down the page. Thanks @AndrewBartel –  sprclldr May 28 '13 at 21:05
    
Note that such loops are typically as $post to set global $post or it needs to be done additionally. setup_postdata() doesn't do this. –  Rarst May 28 '13 at 21:52
    
Great catch @Rarst.. good to know that one. –  sprclldr May 28 '13 at 22:04
add comment

Try it without get_posts(). I currently have a similar function that works like this:

$args = array( 'post_type' => 'customPostName', 'post_status' => 'publish');
$pages = get_pages($args);
foreach ( $pages as $page ) {
    // Do something
    }

Edit: Actually I'm not sure why this isn't working since codex clearly says to use echo $post->ID; with get_posts. http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data

Does this make any difference for you?

    foreach ($auctions as $auction) {
        $option = '<option value="';
        $option .= $auction->ID;
        $option .= '">';
        $option .= $auction->post_title;
        $option .= '</option>';

        echo $option;
share|improve this answer
add comment

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.