Redirect WordPress page to the latest created post by the logged in author/user

Here’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.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)