how can i allow users to view their own pending posts in a front-end page

i want to allow users to view their own pending posts in a front end page but when i access the links created for pending post in the database the links doesn't work until the post is approved If I access the post GUID the one where the status is "publish" the link works, but the the one with the status "pending" does not.

I want to know what is the difference between a pending post and a published one other than their status so I can take those changes into account in order for the pending posts links to work for their authors only.

Topic post-status posts moderation Wordpress

Category Web


By default, the query will display by parameter from the user role (capacity)

If your role dont have capacity to display the preview mode, you will need to add it in query.

The function below will allow only the author (without capacity to preview mode)

/**
 * ALlow the preview pending for post author 
 *
 * @since    1.0.0
 */
function allow_pending_listings($qry) {

    if( is_user_logged_in() ) {

        if ( isset($_GET['p']) ) {

            $post = get_post($_GET['p']); // parameter "p" in url

            // if not in admin and if the post_auhtor is the correct current id
            if ( !is_admin() && $post->post_author == get_current_user_id() ) {

                $qry->set( 'post_status', array('publish', 'pending') );
                // will add the "pending" status to loop query

            }

        }

    }
}
add_action('pre_get_posts','allow_pending_listings');

Use This Code to allow user can view pending post

function allow_pending_listings($qry) {
    if(is_user_logged_in()){
    $edit_data = get_post($_GET['eid']);    
     if (!is_admin() && $edit_data->post_author == $userdata->ID) {
    $qry->set('post_status', array('publish','pending'));
     }
    }
}
add_action('pre_get_posts','allow_pending_listings');

You could try this in your front end php:

    <?php if(is_user_logged_in()): ?>
     <?php $current_user = get_current_user_id(); ?>
       <?php query_posts('post_status=publish,draft&showposts=4&author='.$current_user);
?>
     <?php else: ?>
     <?php query_posts('post_status=publish&showposts=4'); ?>
     <?php endif; ?>

In database difference is just that — the different status in respective column.

The important difference is what that means — WP doesn't consider pending posts public, they won't show up in front end of the site and so on. Messing with this behavior usually goes quite poorly, status system is brittle and full of edge cases.

If you want to deal with items that are in the future (events for example) you should store that date information in post meta, separately from published dates.

If you want to just access pending post by an authorized user you can look into preview functionality, which is automatically exposed in editor for posts that hadn't been published yet.


According to Wordpress Codex is a post status that awaits a user with the publish_posts capability (typically a user assigned the Editor role) to publish. (pending)

In other words, a post that is pending is a post that is not published meaning that it can not be viewed by not registered users with at least the publish_posts capability (Editor etc). So public users can not view the post. That's why the url is not "working".

The database just keeps the post status of the post. WordPress Core is responsible for handling the posts depending their status. Also I would suggest to not alter by no means the database manually and always use the WordPress API to modify WordPress elements like posts, pages etc

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.