Support » Plugins and Hacks » Previous / Next Post navigation by Author

  • hello, my query seems fairly straightforward, i’m sure it can be done through some fantastic hack:

    i run a wordpress blog with a number of authors and would like to provide a link so visitors can access the next / previous post (by Author) on any single.php page.

    at the moment the previous_post / next_post only supports navigation by date or category which is great… is there any possibility this can be extended to become a more advanced option-

    is there a simple hack? would be happy if someone could point me in the right direction.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I needed to do something very similar on one of my own projects. This is what I came up with:

    function get_prev_post_by_author($link="« %link", $title="%title") {
            global $wpdb, $post;
            $prev = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' AND post_author='".$post->post_author."' AND post_date < '".$post->post_date."' ORDER BY post_date DESC LIMIT 1;"));
            if($prev) {
                    $title = preg_replace('/%title/',$prev->post_title, $title);
                    echo preg_replace('/%link/', '<a href="'.get_permalink($prev->ID).'" rel="prev">'.$title.'</a>', $link);
            }
    }                               
    
    function get_next_post_by_author($link="%link &raquo;", $title="%title") {
            global $wpdb, $post;
            $next = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' AND post_author='".$post->post_author."' AND post_date > '".$post->post_date."' ORDER BY post_date ASC LIMIT 1;"));
            if($next) {
                    $title = preg_replace('/%title/',$next->post_title, $title);
                    echo preg_replace('/%link/', '<a href="'.get_permalink($next->ID).'" rel="next">'.$title.'</a>', $link);
            }
    }

    You should be able to drop this into your functions.php file and then use it like the next_post_link and prev_post_link functions, except that this does not have the two optional category parameters.

    that’s fantastic, exactly the kind of idea i was looking for. thanks!

    just wanna say this helped me too, thanks! ๐Ÿ™‚

    can someone help me, to make this work for post navigation by categories instead of author? would be great, because im pretty new in wordpress developing.
    thanks
    anna

    i changed the code from above to this:

    function get_prev_post_by_cat($link="ยซ %link", $title="%title") {
            global $wpdb, $post;
    		$offset = ($paged-1) * $postsperpage;
    
            $prev = $wpdb->get_row($wpdb->prepare("SELECT
    wp_terms.name,
    wp_posts.post_title,
    wp_posts.post_date
    FROM wp_posts
    LEFT JOIN wp_term_relationships
    ON wp_term_relationships.object_id = wp_posts.ID
    LEFT JOIN wp_term_taxonomy
    ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
    LEFT JOIN wp_terms
    ON wp_terms.term_id = wp_term_taxonomy.term_id
    WHERE wp_term_taxonomy.taxonomy = 'category'
    ORDER BY wp_terms.name, wp_posts.post_date DESC LIMIT $offset;"));
            if($prev) {
                    $title = preg_replace('/%title/',$prev->post_title, $title);
                    echo preg_replace('/%link/', '<a href="'.get_permalink($prev->ID).'" rel="prev">'.$title.'</a>', $link);
            }
    }                               
    
    function get_next_post_by_cat($link="%link ยป", $title="%title") {
            global $wpdb, $post;
    		$offset = ($paged-1) * $postsperpage;
            $next = $wpdb->get_row($wpdb->prepare("SELECT
    wp_terms.name,
    wp_posts.post_title,
    wp_posts.post_date
    FROM wp_posts
    LEFT JOIN wp_term_relationships
    ON wp_term_relationships.object_id = wp_posts.ID
    LEFT JOIN wp_term_taxonomy
    ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
    LEFT JOIN wp_terms
    ON wp_terms.term_id = wp_term_taxonomy.term_id
    WHERE wp_term_taxonomy.taxonomy = 'category'
    ORDER BY wp_terms.name, wp_posts.post_date ASC LIMIT $offset ;"));
            if($next) {
                    $title = preg_replace('/%title/',$next->post_title, $title);
                    echo preg_replace('/%link/', '<a href="'.get_permalink($next->ID).'" rel="next">'.$title.'</a>', $link);
            }
    }

    but i think i have to include an offset parameter somehow?

    please help me ๐Ÿ™

    greetings
    anna

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Previous / Next Post navigation by Author’ is closed to new replies.