WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get prev and next post of current logged in user? (2 posts)

  1. nicolaelvin
    Member
    Posted 1 year ago #

    I have two different types of posts pages. I have a post page that always shows the posts of the site author and I have a posts page that shows the current logged in users posts. I want the next and prev links on each page to only get the next and prev posts of either site author or current logged in user, depending on what page we are on. I had a similar requirement with archives - i.e. only get those archives for current users, and someone kindly helped me out with that here. SO I figure it's a similar sort of thing, I need to use filters?

  2. nicolaelvin
    Member
    Posted 1 year ago #

    I have solved with help from the answer to my other similar question.

    Add this where you want the prev and next post links to appear:

    global $archive_author_id;							$archive_author_id = get_current_user_id();
    
    add_filter('get_previous_post_where', 'get_prev_post_where_filter');							previous_post('%','previous post','no');
    							add_filter('get_next_post_where', 'get_next_post_where_filter');
    next_post('%','next post','no');

    And where in your functions.php file:

    function get_prev_post_where_filter($where) {
       global $archive_author_id;
       return $where .' AND post_author = ' . $archive_author_id;
    }
    
    function get_next_post_where_filter($where) {
       global $archive_author_id;
       return $where .' AND post_author = ' . $archive_author_id;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.