我正在使用 wordpress 开发一个门户网站。
我需要从前端访问草稿帖子。 如果我使用管理员帐户登录,我可以在单页中查看草稿帖子(管理员帖子)。但其他用户无法从前端访问自己的草稿并收到 404 错误。
首页上的链接如 http://website.com/?p=486
我不确定问题出在哪里。也许是访问限制或永久链接设置或404设置或其他。有什么想法吗?
我找到了解决方案。问题是单页仅显示已发布的帖子。 通过使用 wordpress pre_get_posts 钩子,我更改了查询并添加了草稿帖子。
function ceo_single_page_published_and_draft_posts( $query ) {
if( is_single() ) {
$query->set('post_status', 'publish,draft');
}
}
add_action('pre_get_posts', 'ceo_single_page_published_and_draft_posts');
对我有用的解决方案是查询已发布和草稿:
query_posts(array(
'post_status' => array( 'publish', 'draft')
));
将其放入functions.php中,它将允许用户在前端看到他们的草稿,但只有他们的草稿并且只有他们(以及任何具有适当功能的其他人,我相信默认情况下只有管理员)才能看到它们,所以他们不向全世界公开。
function allow_user_to_preview_draft_posts( $query ) {
if (is_single() && is_user_logged_in()) {
if (isset($_GET['p'])) {
$the_id = $_GET['p'];
$author = get_post_field( 'post_author', $the_id );
if (get_current_user_id() == $author) {
$query->set('post_status', 'publish,draft');
}
}
}
}
add_action('pre_get_posts', 'allow_user_to_preview_draft_posts');