WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get posts from a specific user. (6 posts)

  1. cip6791
    Member
    Posted 3 years ago #

    Hello,

    I am trying to get the posts from a logged in user.

    This code displays the user that is logged in and the user id.

    if ( is_user_logged_in() ) {
    
    global $current_user;
          get_currentuserinfo();
    
        echo 'Welcome, registered user!';
    echo 'User ID: ' . $current_user->ID . "\n";
    } else {
        echo 'Welcome, visitor!';
    };

    Welcome, registered user!User ID: 1

    How can I get all the posts that the user with the ID:1 has posted?
    Thank you

  2. esmi
    Forum Moderator
    Posted 3 years ago #

  3. cip6791
    Member
    Posted 3 years ago #

    <?php
    if ( is_user_logged_in() ) {
    
    global $current_user;
          get_currentuserinfo();
    
    echo 'User ID: ' . $current_user->ID . "\n";
    
    //The Query
    query_posts('author=2');
    
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    the_title();
    echo "<br>";
    endwhile; else:
    echo "The user has not contributed anything!";
    endif;
    
    //Reset Query
    wp_reset_query();
    
    } else {
        echo 'Welcome, visitor!';
    };
    ?>

    Ok .. this is what I have so far. I have one problem here: query_posts('author=2'); How do I replace the 2 with the current user ID?

  4. chinmoy
    Member
    Posted 3 years ago #

    query_posts('author='.$current_user->ID );

  5. cip6791
    Member
    Posted 3 years ago #

    Nevermind ... I figured it out. Had to define it in $args. :)

    thanks Esmi & chinmoy29

  6. cip6791
    Member
    Posted 3 years ago #

    Now if I would want to create an edit and delete link ...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags