You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现作者专属的WordPress自定义文章类型待审核文章预览?

Solution: Allow Authors to Preview Their Own Pending Custom Post Type Posts

To let authors preview their own pending "deals" posts while keeping access restricted to others, you can add custom code to your WordPress site. Here's a step-by-step implementation:

Step 1: Add the Custom Code

Add this code to your theme's functions.php file or a custom plugin (recommended for better maintainability):

// Allow authors to preview their own pending 'deals' posts
add_action('template_redirect', 'allow_author_preview_pending_deals');

function allow_author_preview_pending_deals() {
    // Skip if we're not dealing with the 'deals' post type
    if (get_query_var('post_type') !== 'deals') {
        return;
    }

    // Get the post ID from the request
    $post_id = get_query_var('p');
    if (!$post_id) {
        return;
    }

    // Fetch the post object
    $post = get_post($post_id);
    if (!$post || $post->post_type !== 'deals') {
        return;
    }

    // Only apply to pending posts
    if ($post->post_status !== 'pending') {
        return;
    }

    $current_user = wp_get_current_user();

    // Redirect non-logged-in users to 403
    if (!$current_user->ID) {
        wp_redirect(home_url('/403'), 403);
        exit;
    }

    // Redirect users who aren't the post author to 403
    if ($post->post_author !== $current_user->ID) {
        wp_redirect(home_url('/403'), 403);
        exit;
    }

    // Adjust the query to load the pending post for the author
    global $wp_query;
    $wp_query->set('post_status', 'pending');
    $wp_query->is_single = true; // Ensure the single post template is used
}

// Grant read capability for own pending 'deals' posts
add_filter('user_has_cap', 'grant_author_access_to_own_pending_deals', 10, 3);

function grant_author_access_to_own_pending_deals($allcaps, $caps, $args) {
    // Check if we're checking read access to a post
    if ($args[0] !== 'read_post') {
        return $allcaps;
    }

    $post_id = $args[2];
    $post = get_post($post_id);

    // Verify it's a pending 'deals' post
    if ($post && $post->post_type === 'deals' && $post->post_status === 'pending') {
        $current_user = wp_get_current_user();
        // Grant read access if user is the author
        if ($current_user->ID === $post->post_author) {
            $allcaps['read_post'] = true;
        }
    }

    return $allcaps;
}

How It Works

  1. Template Redirect Check: The template_redirect hook runs before WordPress loads the page template. We:

    • Confirm the request is for the 'deals' post type.
    • Fetch the post and check if it's pending.
    • Verify the current user is logged in and is the post's author.
    • Adjust the query to load the pending post (since WordPress doesn't include pending posts in the default query for non-admins).
  2. Capability Filter: The user_has_cap filter ensures the author has the read_post capability specifically for their own pending 'deals' posts, which WordPress normally restricts.

Testing the Solution

  • Log in as an author, create a pending 'deals' post, and visit the link http://localhost/project/?post_type=deals&p=YOUR_POST_ID – you should see the post preview.
  • Log in as a different user (non-admin, non-author of the post) and try the same link – you'll be redirected to a 403 error page.
  • Admins can still access all pending posts as usual.

Optional: Custom 403 Page

If you don't have a 403 page, you can create one in WordPress (a page with slug '403') and customize its content to inform users they don't have access. Alternatively, redirect to the homepage instead of /403.

内容的提问来源于stack exchange,提问作者Pukhraj Suthar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:57:18