0

I have created custom post type gallery.

How change the previous post and next post link to point to the current Authors posts only?

The Previous and Next Post link should not display if there are no more posts

enter image description here

5

The correct method of doing this since Wordpress 4.4 is simply the following:

add_filter( "get_next_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
  $where .= " AND p.post_author='".$post->post_author."'";
  return $where;
}, 10, 5);

add_filter( "get_previous_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
  $where .= " AND p.post_author='".$post->post_author."'";
  return $where;
}, 10, 5);

Note: Please do not follow levidia1221's accepted response, as it involves modifying Wordpress Core, which is a terrible idea. Modifying the core means you can't update Wordpress without losing your changes, which is an enormous security risk. If there are no hooks present in a function you want to modify, it's better to copy the functions you need and change them in your theme or plugin, rather than modifying the core files directly.

-2

To limit post navigation to the same author and category, locate the link-template.php file in the /wp-includes/ folder, and edit using your favorite text editor (Notepad++ for me). In this file you will alter four functions. Locate the following lines in the link-template.php file:

function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
}

Change that to the following:

function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '', $is_author = false) {
    adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true, $is_author);
}

Below the previous lines of code you should find this:

function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
}

Change that to the following:

function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '',$is_author = false) {
    adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false, $is_author);
}

These changes are small. You should notice that the only changes made were adding $is_author = false to the function parameters and $is_author to the call to adjacent_post_link call.

On the next function you'll be adding another parameter and adjust the SELECT query that is pulling in the next/previous post. Locate the following line that should look something like this:

function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
    ...
    }

This block is fairly long so I'm only covering what's being changed. On this line add the same parameter as before ($is_author = false):

function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true, $is_author = false) {
    ...
    }

Next, in this same function find the $where variable as shown below (roughly 50 lines into the function).

$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );

What we want to do next is alter this variable so it retains the author only when $is_author is true. To do this test the boolean value of $is_author and tack a little extra on the end of the $where variable.

$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
    if($is_author)
        $where .= " AND p.post_author='".$post->post_author."'";

For the final change in the link-template.php file locate the following function:

function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);

Alter this function by the $is_author in the adjacent_post_link function and adding it to the get_adjacent_post call:

function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true, $is_author = false) {
    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous, $is_author);

Now you're ready to use this new option in the theme files. To "activate" the retain author function use something like this in your single.php:

<?php previous_post_link( '%link', '' . _x( '&larr;', 'Previous post link', 'twentyten' ) . ' %title','true','','true' ); ?>
<?php next_post_link( '%link', '%title ' . _x( '&rarr;', 'Next post link', 'twentyten' ) . '','true','','true' ); ?>

Setting the last value to "true" activates the retain author function for that link. This will ensure the link stays within the same author as well as the same date.

  • Agree with the answer above, modifying core is never a good idea for the same reasons provided by @Cin. – Nathan Apr 19 '18 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.