WordPress.org

Support

Support » Themes and Templates » [Resolved] Resetting of postdata, still getting same data after second setup_postdata

[Resolved] Resetting of postdata, still getting same data after second setup_postdata

  • Hi, I’m in the process of transforming static HTML to a WordPress template. I’ve got most things working, but need some help on a subject that’s threatening my sanity ;-).

    Due to the way the design has been implemented, I can’t use a foreach-loop to present the stories. I need to fetch them one at a time. At the start of index.php I threfore have

    $postar = get_posts(array('numberposts' => 4));

    Then I’ve used
    setup_postdata($postar[0]); followed by using the_title() etc and then repeating the process somewhere else in the same file with setup_postdata($postar[1]); etc.

    The problem I’ve got is that everything is perfect after the first setup_postdata, but after the second or third I still get the data from $postar[0], with the exception of the_content() which returns the content from $postar[1]!

    To debug, Ive’ added a part:

    echo "*Direkte frå objektet:\n"
    echo $postar[0]->post_title."\n";
    echo $postar[1]->post_title."\n";
    echo "*Frå wp-funksjonar:\n"
    setup_postdata($postar[0]);
    the_title();
    setup_postdata($postar[1]);
    the_title(); ?>
    echo "*Frå print_r:\n"
    print_r($postar[0]);
    print_r($postar[1]);

    This returns:

    *Direkte frå objektet:
    Testartikkel1
    Hello World!
    *Frå wp-funksjonar:
    Testartikkel1
    Testartikkel1
    *Frå print_r:
    stdClass Object
    (
        [ID] => 3
        [post_author] => 1
        [post_date] => 2011-02-27 17:59:46
        [post_date_gmt] => 2011-02-27 17:59:46
        [post_content] => freopfjrejfre ofjrep fjrep fje rf
        [post_title] => Testartikkel1
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] =>
        [post_name] => testartikkel1
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2011-02-27 18:00:21
        [post_modified_gmt] => 2011-02-27 18:00:21
        [post_content_filtered] =>
        [post_parent] => 0
        [menu_order] => 0
        [post_type] => post
        [post_mime_type] =>
        [comment_count] => 0
        [filter] => raw
    )
    stdClass Object
    (
        [ID] => 1
        [post_author] => 1
        [post_date] => 2011-02-27 17:43:40
        [post_date_gmt] => 2011-02-27 17:43:40
        [post_content] => Dette er ein testartikkel
        [post_title] => Hello, World!
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] =>
        [post_name] => helloworld
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2011-02-27 17:43:40
        [post_modified_gmt] => 2011-02-27 17:43:40
        [post_content_filtered] =>
        [post_parent] => 0
        [menu_order] => 0
        [post_type] => post
        [post_mime_type] =>
        [comment_count] => 0
        [filter] => raw
    )

    I’ve attempted using wp_reset_postdata(), but that doesn’t help me in any way, still the same result.

Viewing 9 replies - 1 through 9 (of 9 total)
  • This is still unresolved.. any hints here would be great 🙂

    Chip Bennett

    @chipbennett

    Theme Review Admin

    I’m not sure I understand the issue. The two outputs appear to be different (the first one is PostID 3, and the second one is PostID 1).

    Yes, in the array they are different, but that’s when calling print_r. When using setup_postdata and then the_content etc (the proper way of doing things), I only get content from $postar[0], even though I’ve specified setup_postdata($postar[1]).

    Chip Bennett

    @chipbennett

    Theme Review Admin

    Oh, I see now.

    What happens if you try to call post content as an object? e.g.:

    $postar = get_posts(array('numberposts' => 4));
    $postar[0]->post_content;
    $postar[1]->post_content;

    When I try to call content as an object, everything looks fine, with proper output from the different objects:

    echo $postar[0]->post_title."\n";
    echo $postar[1]->post_title."\n";

    returns:

    Testartikkel1
    Hello World!

    But

    setup_postdata($postar[0]);
    the_title();
    setup_postdata($postar[1]);
    the_title();

    returns:

    Testartikkel1
    Testartikkel1

    I’ve shortened index.php considerably to only include debuginformation. The code: pastebin.com

    The site in use: wordpress site with the index.php in question

    Chip Bennett

    @chipbennett

    Theme Review Admin

    If you can use the object parameters (i.e. wp-posts table columns) successfully, why worry about outputting via setup_postdata()?

    You’ve already called the object, so at that point I don’t think there are any performance gains or hits with using $post->post_content vs. the_content().

    By not using static references I can more reasily fetch categories, name of poster, formatted dates etc – information not available in the array/object.

    Solved by adding a global $post and referencing to $post, not $postar.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Resetting of postdata, still getting same data after second setup_postdata’ is closed to new replies.