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?
It took me a while but I got it! This works as follows:
This works swell because it gives the appearance of a working permalink. It acts as a pseudo permalink.
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!
I’ve been working on this issue recently, and I would like to post my solution, if the 404 override doesn’t work.
Basically WordPress set pending post status to private instead of public (that is why you’re not able to see it, if you’re not logged in as admin), so we need to reset it: