admin管理员组文章数量:1143277
I have a basic WordPress code that redirects someone to the last post type created on the site, in this case is the "case" post type:
<?php
/*
Template Name: Redirect
*/
$args = array(
'posts_per_page' => '1',
'post_type' => 'case'
);
$post = get_posts($args);
if($post){
$url = get_permalink($post[0]->ID);
wp_redirect( $url, 301 );
exit;
}
I'm trying to expand on this code so I redirect people to their own created posts only, so they will be redirected to the last post they created with an added parameter like ?mode=edit to the end of the url and if they're not logged in, they are going to be sent to a pre-defined error page, like domain/error.
I'm a bit new to PHP and for the life of me, I can't figure it out. Any input or hints are appreciated.
I have a basic WordPress code that redirects someone to the last post type created on the site, in this case is the "case" post type:
<?php
/*
Template Name: Redirect
*/
$args = array(
'posts_per_page' => '1',
'post_type' => 'case'
);
$post = get_posts($args);
if($post){
$url = get_permalink($post[0]->ID);
wp_redirect( $url, 301 );
exit;
}
I'm trying to expand on this code so I redirect people to their own created posts only, so they will be redirected to the last post they created with an added parameter like ?mode=edit to the end of the url and if they're not logged in, they are going to be sent to a pre-defined error page, like domain.com/error.
I'm a bit new to PHP and for the life of me, I can't figure it out. Any input or hints are appreciated.
Share Improve this question edited Oct 29, 2023 at 6:50 Sean Bagheri asked Oct 29, 2023 at 6:21 Sean BagheriSean Bagheri 11 bronze badge1 Answer
Reset to default 0Here's the modified code to achieve this:
<?php
/*
Template Name: Redirect
*/
// Check if the user is logged in
if (is_user_logged_in()) {
// Get the current user's ID
$user_id = get_current_user_id();
// Query for the last post created by the user in the "case" post type
$args = array(
'posts_per_page' => 1,
'post_type' => 'case',
'author' => $user_id,
'orderby' => 'date',
'order' => 'DESC',
);
$posts = get_posts($args);
if (!empty($posts)) {
// Get the last post created by the user
$post = $posts[0];
// Construct the redirect URL with the "?mode=edit" parameter
$url = get_permalink($post->ID) . '?mode=edit';
// Redirect the user
wp_redirect($url, 301);
exit;
} else {
// Redirect the user to a predefined error page
wp_redirect('http://yourdomain.com/error', 301);
exit;
}
} else {
// Redirect the user to a predefined error page since they are not logged in
wp_redirect('http://yourdomain.com/error', 301);
exit;
}
?>
In this code, we first check if the user is logged in using is_user_logged_in()
. If the user is logged in, we proceed to query the last post created by the user in the "case" post type and construct the redirect URL. If the user is not logged in, we redirect them to the predefined error page.
本文标签: phpRedirect WordPress page to the latest created post by the logged in authoruser
版权声明:本文标题:php - Redirect WordPress page to the latest created post by the logged in authoruser 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736761278a1951559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论