WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying post revisions as seperate posts (4 posts)

  1. xanderashwell
    Member
    Posted 4 years ago #

    Hi guys,

    I've got a question that I haven't been able to find a solution to elsewhere, so I thought it might be worth putting the problem out there, in the hope that someone might be able to point me in the right direction... I've got a basic understanding of PHP, so any point in the right direction would be great!

    I'm basically looking to display the stored post revisions of a specific post as a sort of historical reference of the updated post in question...with each post revision being displayed as a full, seperate sub-post. I know that the wp_list_post_revisions call brings up an unordered list of previous revisions, but can't seem to find any documentation of how to pull the data from the database and render it on the page.

    Any help would be gratefully received!

    Cheers,

    Xander

  2. Mike Little
    Member
    Posted 4 years ago #

    Hi Xander,

    wp_get_post_revisions() looks like a good starting place. That will return you a list of the revisions of a post as an array of post objects.

    You should then be able to write a loop around that array.
    If you want to display them one at a time, then you will probably need to get a little fancy in your code, but I hope this is a pointer in the right direction.

    Mike

  3. xanderashwell
    Member
    Posted 4 years ago #

    Hi Mike,

    Many many thanks for your assistance, I think that is the relevant snippet of code that I'm looking for, and thanks for pointing it out to me!

    I can't seem to get it to function in a loop though... I can call up wp_list_post_revisions and get that to return a list (which links through to the relevant page's revisions in the back end, but wp_get_post_revisions doesn't return anything within a page loop. Perhaps I'm not calling it in the right place? `<?php the_post() ?>
    <div id="entry-content" class="exposeit">
    <div id="post-<?php the_ID() ?>" class="<?php sandbox_post_class() ?>">
    <h2 class="entry-title"><?php the_title() ?></h2>

    <?php the_content() ?>
    <?php wp_get_post_revisions() ?>`

    My apologies if I've misunderstood your suggestion...

    Thanks again for your generous assistance,

    Xander

  4. xanderashwell
    Member
    Posted 4 years ago #

    Just as a follow-up to this query, Mike has got back to me with this solution:

    <?php $revs = wp_get_post_revisions($id, array('order'=> 'ASC'));
          //echo "<pre>" . var_export( $revs, true ) . "</pre>";
          if (count($revs > 0)) {
              echo "<h2>Older versions</h2>";
    
              foreach ($revs as $rev) { ?>
                <div class="post revision" id="post-<?php echo $rev->ID; ?>">
                <h3><?php echo apply_filters( 'the_title', $rev->post_title, $rev->ID ); ?></h3>
                        <p class="postmetadata">
                            <small>This revision was saved
                              on <?php echo mysql2date('l, F jS, Y', $rev->post_date) ?>
                              at <?php echo mysql2date('H:m:s', $rev->post_date)  ?>
                            </small>
                        </p>
                    <div class="entry">
                        <?php echo apply_filters('the_content', $rev->post_content); ?>
                    </div>
                </div>
             <?php } /* end foreach */
          } /* end if any revisions */
             ?>

    Which, of course, works perfectly. Mike's left a debug echo commented out at the top of the loop. Enabling that will show you all the post structure you can get at to display.

    Thanks Mike! I'll mark this thread as "resolved".

    Xander

Topic Closed

This topic has been closed to new replies.

About this Topic