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 display the latest post revision if the page is "pending", by default the page displays a 404 if it is pending. But I want it to show the latest revision until the page is updated.

The code below doesn't seem to display anything but the title, I have used a hook to the wp_insert_post_data function which changes the status of the post to "pending" if the users role isn't admin.

The post is set to pending in the database, but I cannot get the below code to show the latest revision?

I also don't know how to prevent it from redirecting to a 404 page? If I am logged in it will show the post otherwise a 404 page.

    <?php while (have_posts()) : the_post(); ?>    
            <h1><?php the_title(); ?></h1>                          
        <?php 
        echo get_post_status(); //echo's pending
        if(get_post_status() == 'pending') {
            $query = new WP_Query(array('post_type' => 'revision', 'post_parent' => '7', 'posts_per_page' => 1)); // 7 is the current page ID
            if ($query->have_posts()) {                 
                while ($query->have_posts()) {                      
                    $query->the_post();                     
                    the_content();
                    echo $post->ID;
                }
            }
            wp_reset_postdata();
        } else {
            the_content(); 
        }               
        ?>           
    <?php endwhile; ?>
<?php endif; ?>

Any advice? Is there an easier way to show the revision if the page is pending?

Thanks !

Edit:

Okay I seem to have it working using the code below:

if(get_post_status() == 'pending') {
   $query = 'SELECT ID FROM `wp_posts` WHERE `post_parent` = '.$post->ID.' AND `post_type` = "revision" AND `post_status` = "pending" AND `post_title` <> "Auto Draft" AND TRIM(`post_content`) <> "" AND `post_name` NOT LIKE "%autosave%" ORDER BY `post_modified` DESC LIMIT 1';
   $revision_id = $wpdb->get_var($query);
   $revision = wp_get_post_revision($revision_id);
   $content = $revision->post_content;              
   $content = apply_filters('the_content', $content);
   $content = str_replace(']]>', ']]>', $content);
   echo $content;
   wp_reset_postdata();
} else {
   the_content(); 
}

So now the question is - How can I prevent a page which is set as pending to redirecting to the 404 page?

share|improve this question
    
Possibly relevant Are there any hooks that alter the 404 logic? –  Rarst Sep 14 '11 at 12:28
    
@Rarst thanks for that, although I just tried and had no luck :( –  Elliott Sep 14 '11 at 12:39

1 Answer 1

It took me a while but I got it! This works as follows:

  1. Since you have no reference to the post other than the URI, we segment out the URI to check for a page with that page_name
  2. If there is a page, we get the latest revision child
  3. Hack the query to force our revision slug to source the page

This works swell because it gives the appearance of a working permalink. It acts as a pseudo permalink.

add_action( 'wp', 'override_404' );
function override_404($query) {

    if(is_404):

        $uri = trim($_SERVER['REQUEST_URI'], '/');
        $segments = explode('/', $uri);
        $slug_index = count($segments);

        $page_slug = $segments[$slug_index - 1];
        $page = get_page_by_path($page_slug, OBJECT, 'page');

        $revision_args = array('post_parent' => $page->ID, 'post_type' => 'revision', 'post_status' => 'inherit', 'numberposts' => 1);
        $revision = array_shift(get_children($revision_args));

        if($revision):
            $query->query_vars['pagename'] = $revision->page_name;          
            $query->query_string = "pagename={$revision->page_name}";
            $query->request = $revision->page_name;
            $query->matched_rule = "({$revision->page_name})(/[0-9]+)?/?$";
            $query->matched_query = "pagename={$revision->page_name}&page=";
            $query->did_permalink = 1;
        endif;

    endif;

    return $query;
}

The only caveat is sometimes the slug does not save until you publish the page. In order for this to work correctly, the slug must be in the database. Cheers!

share|improve this answer
    
Hi, thanks for this although I have tried it and it doesn't seem to work? I echo'd some test data in the if($revision) but it returned the data for everypage even if it isn't a revision? –  Elliott Sep 15 '11 at 8:15
    
Try publishing the page first and then setting it to pending. Also what is your permalink structure. Use print_r to test revision –  Brian Fegter Sep 15 '11 at 12:51
    
The structure is Month and name, I tried print_r and the post_type was still revision even if the page was published. –  Elliott Sep 15 '11 at 13:36
    
I created an example here: coderrr.com/revision-page This page is not published, but is pending review. I appended the slug here. If you type coderrr.com/?s=revision no results show so this proves it's working on my site. Currently the post content box for the page has no content in the editor, so I know it's pulling from the revision. –  Brian Fegter Sep 15 '11 at 13:49
    
The two issues that come to mind are these pages won't be included in your search results, nor in your search engine indexing if you're using a plugin like Google XML Sitemaps. –  Brian Fegter Sep 15 '11 at 14:24

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.