How to Get Current Logged-in User’s Post with Edit Link in WordPress?

To display the currently logged-in user’s posts along with an edit link in WordPress, you’ll need to use a combination of WordPress functions.

Here’s a step-by-step guide on how you can achieve this:

  1. Open Your Theme’s Functions File: The first step is to open your theme’s functions.php file. You can access it via the WordPress Dashboard by going to Appearance > Theme Editor > Theme Functions or directly via FTP.

  2. Write a Custom Function: Next, you’ll need to create a custom function that fetches the current user’s posts and displays them with an edit link. You can add the following code to your functions.php:

function display_user_posts_with_edit_link() {
    // Check if user is logged in
    if ( is_user_logged_in() ) {
        // Get current user
        $current_user = wp_get_current_user();

        // Get current user's posts
        $args = array(
            'author' => $current_user->ID,
            'post_type' => 'post',
            'post_status' => 'publish'
        $user_posts = get_posts( $args );

        // Loop through each post and display it with an edit link
        foreach ( $user_posts as $post ) {
            setup_postdata( $post );
            echo '<h2><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></h2>';
            echo '<p>' . get_the_excerpt() . '</p>';
            echo '<a href="' . get_edit_post_link( $post->ID ) . '">Edit Post</a>';

This function checks if the user is logged in, fetches their posts, and displays each post title (linked to the post itself), excerpt, and an ‘Edit Post’ link.

  1. Call the Function: Now, you can use this function wherever you want to display the current user’s posts with edit links. You can add the following code to any template file where you want to display the posts:
<?php display_user_posts_with_edit_link(); ?>

This could be in a sidebar, a special page template, or even in single.php if you want to display the user’s other posts at the end of each single post.

Remember to save all changes to your files before exiting.

Leave a Comment