WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Function get_next_post_by_author modification (6 posts)

  1. amatsu
    Member
    Posted 3 years ago #

    Hello. I'm using this custom function I found here: http://wordpress.org/support/topic/previous-next-post-navigation-by-author?replies=7

    function get_next_post_by_author($link="%link »", $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);
            }
    }

    This gets the next post by the same author.

    I would like to include the $in_same_cat filter in that function, just like in the normal next_post_link function, but I don't know where to put that.

    Thanks.

  2. Sidney Harrell
    Member
    Posted 3 years ago #

    Any reason this wouldn't work?

    function get_next_post_by_author($link="%link &raquo;", $title="%title") {
            global $wpdb, $post;
            $post_categories = wp_get_post_categories( $post->id );
            $next_date=$post->post_date;
            do {
                $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 > '".$next_date."' ORDER BY post_date ASC LIMIT 1;"));
                $next_categories = wp_get_post_categories( $next->id );
                $next_date=$next->post_date;
                foreach($post_categories as $pc){
                    foreach($next_categories as $nc){
                        if($pc==$nc) {
                            $match=$next;
                        }
                    }
                }
            } while(!$match && $next);
            if($match) {
                    $title = preg_replace('/%title/',$match->post_title, $title);
                    echo preg_replace('/%link/', '<a href="'.get_permalink($match->ID).'" rel="next">'.$title.'</a>', $link);
            }
    }
  3. amatsu
    Member
    Posted 3 years ago #

    Thanks for the reply. It doesn't seem to be working as expected. The single.php takes a long time to load and when it does there are some errors:

    Fatal error: Maximum execution time of 30 seconds exceeded in C:\AppServ\www\wordpress\wp-includes\wp-db.php on line 1317

  4. Sidney Harrell
    Member
    Posted 3 years ago #

    Ok, I got it figured out. There were 3 problems with my previous code.
    1. I used $post->id when I was supposed to use $post->ID. It wasn't picking up the array of catagory IDs, so of course wasn't making any matches.
    2. I modified the test for a catagory match to exclude catagory 1, which is shared by all posts.
    3. The line to pull the post data out of the database only pulled the post ID and title, not the post_date, so it was going into an infinite loop.
    Here's the updated code:

    function get_next_post_by_author($link="%link &raquo;", $title="%title") {
            global $wpdb, $post;
            $post_categories = wp_get_post_categories( $post->ID );
            $next_date=$post->post_date;
            do {
                $next = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title, post_date FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' AND post_author='".$post->post_author."' AND post_date > '".$next_date."' ORDER BY post_date ASC LIMIT 1;"));
                $next_categories = wp_get_post_categories( $next->ID );
                $next_date=$next->post_date;
                foreach($post_categories as $pc){
                    foreach($next_categories as $nc){
                        if($pc>1&&$pc==$nc) {
                            $match=$next;
                        }
                    }
                }
    
            } while(!$match && $next);
            if($match) {
                    $title = preg_replace('/%title/',$match->post_title, $title);
                    echo preg_replace('/%link/', '<a href="'.get_permalink($match->ID).'" rel="next">'.$title.'</a>', $link);
            }
    }

    I did a little testing on it, and it looks like it works now, but let me know if it works for you.

  5. amatsu
    Member
    Posted 3 years ago #

    It does work now, thank you, that was very helpful.

    By the way, what do I need to change to get the previous post instead of the next?

    Thanks again.

  6. Sidney Harrell
    Member
    Posted 3 years ago #

    Basically, just change the greater than in the sql command to less than, and the order by ascending date to order by descending date. But I changed all the next to previous.

    function get_previous_post_by_author($link="%link &raquo;", $title="%title") {
            global $wpdb, $post;
            $post_categories = wp_get_post_categories( $post->ID );
            $previous_date=$post->post_date;
            do {
                $previous = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title, post_date FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' AND post_author='".$post->post_author."' AND post_date < '".$previous_date."' ORDER BY post_date DESC LIMIT 1;"));
                $previous_categories = wp_get_post_categories( $previous->ID );
                $previous_date=$previous->post_date;
                foreach($post_categories as $pc){
                    foreach($previous_categories as $nc){
                        if($pc>1&&$pc==$nc) {
                            $match=$previous;
                        }
                    }
                }
    
            } while(!$match && $previous);
            if($match) {
                    $title = preg_replace('/%title/',$match->post_title, $title);
                    echo preg_replace('/%link/', '<a href="'.get_permalink($match->ID).'" rel="previous">'.$title.'</a>', $link);
            }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.