WordPress.org

Ready to get started?Download WordPress

Forums

Previous / Next Post navigation by Author (7 posts)

  1. callumalden
    Member
    Posted 4 years ago #

    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.

  2. eryanv
    Member
    Posted 4 years ago #

    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.

  3. callumalden
    Member
    Posted 4 years ago #

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

  4. jpgnfx
    Member
    Posted 3 years ago #

    just wanna say this helped me too, thanks! :)

  5. Dummbatz
    Member
    Posted 3 years ago #

    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

  6. Dummbatz
    Member
    Posted 3 years ago #

    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?

  7. Dummbatz
    Member
    Posted 3 years ago #

    please help me :(

    greetings
    anna

Topic Closed

This topic has been closed to new replies.

About this Topic