0

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 enter image description here 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.

6 Answers 6

2

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');
1

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

2
  • i actually want to view the pending post just by the author (current user) i don't want it to be viewed by any other user , is there's a way ? Commented Feb 14, 2017 at 16:26
  • 1
    @ouqasmohamed Please edit your question to clarify what you're trying to do (ie, allow users to view their own pending posts in a front-end page).
    – Pat J
    Commented Feb 14, 2017 at 16:52
1

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');
0

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.

7
  • do you mean the preview functionality from the dashboard ? i don't get it Commented Feb 14, 2017 at 16:38
  • Yes, I do mean that.
    – Rarst
    Commented Feb 14, 2017 at 16:41
  • i know that i can preview it in the dashboard , i'm wondering if i can preview in the site just like any other post but it's visible only for the author Commented Feb 14, 2017 at 16:49
  • Just take a preview link and try? There should be API function to retrieve it by code as far as I remember.
    – Rarst
    Commented Feb 14, 2017 at 17:01
  • the preview link created for the post in the database does not work with status is pending , but the same link works if the post is approved Commented Feb 14, 2017 at 17:10
0

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; ?>
7
  • this is goona work for pending posts ? Commented Feb 15, 2017 at 16:06
  • It should, if not change: <?php query_posts('post_status=publish,draft&showposts=4&author='.$current_user); ?> to <?php query_posts('post_status=publish,draft,pending&showposts=4&author='.$current_user); ?>
    – Heather
    Commented Feb 15, 2017 at 16:07
  • No problem, let me know how it goes.
    – Heather
    Commented Feb 15, 2017 at 16:09
  • it's not exactly what i need , what i need is to show the pending post in the front page that shows all other posts (published ) , this page already exist , the problem is im retrieving all pending and published post and im making a link on their title , but the links of the published ones works but for the pending ones does not it gives a 404 Commented Feb 15, 2017 at 16:16
  • Can you give me an example of a link that gives a 404? I can actually preview my pending posts in the front end by clicking the Permalink within the Admin area of the post.
    – Heather
    Commented Feb 15, 2017 at 16:28
0

This seems to be an issue with post statuses marked as protected, which only allows users with edit caps on the post to preview them.

For these cases, this should do the trick:

add_filter( 'posts_results', function( $posts, $wp_query ) {
    if ( ! empty( $posts ) && ( $wp_query->is_single || $wp_query->is_page ) ) {
        $post = get_post( $posts[0] );
        if ( (int) $post->post_author === get_current_user_id() ) {
            $status = get_post_status_object( $post->post_status );

            if ( $status && $status->protected && in_array( $status->name, [ 'draft', 'pending', 'expired', 'rejected' ], true ) ) {
                $handler = function( $posts, $wp_query ) use ( $post, &$handler ) {
                    remove_filter( 'the_posts', $handler, 100, 2 );
                    $posts[] = $post;
                    return $posts;
                };

                add_filter( 'the_posts', $handler, 100, 2 );
            }
        }
    }

    return $posts;
}, 100, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.