WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#27564 closed defect (bug) (duplicate)

previous_post_link() and next_post_link() returns nothing

Reported by: genkisan Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9
Component: Themes Keywords: has-patch
Focuses: template Cc:
PR Number:

Description

Tested on clean install of WP 3.9 beta 2 with no plugins activated.

Case 1: previous_post_link/next_post_link returns nothing when "in_same_cat" parameter is true
In /themes/twentyfourteen/inc/template-tags.php, if you replace

previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ) );
next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ) );

with

previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ), true );
next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ), true );

, the previous/next links are not displayed while they worked correctly on WP 3.8.1.

Case 2: previous_post_link/next_post_link returns nothing when "excluded_terms" parameter is specified
In /themes/twentyfourteen/inc/template-tags.php, if you replace

previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ) );
next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ) );

with

previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ), false, '2' );
next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ), false, '2');

, again the previous/next links are not displayed while they worked correctly on WP 3.8.1.

Attachments (1)

27564.diff (523 bytes) - added by jeremyfelt 6 years ago.

Download all attachments as: .zip

Change History (9)

#1 @jeremyfelt
6 years ago

  • Milestone changed from Awaiting Review to 3.9

Hi @genkisan, thanks for the report.

I cannot confirm the first scenario listed in current trunk. It's possible that this has been resolved.

I can confirm the second scenario. If any argument is passed for excluded_terms, no link will be generated. This was introduced in [27285]

#2 @jeremyfelt
6 years ago

  • Milestone changed from 3.9 to Awaiting Review

Unconfirming this for a bit until I can confirm it again. :)

@jeremyfelt
6 years ago

#3 @jeremyfelt
6 years ago

Ok, confirmed.

In WP_Adjacent_Post::get_post(), the taxonomy query looks for slugs. The excluded terms passed to get_adjacent_post() are expected as an Array or comma-separated list of excluded terms IDs.. When an ID of 4 is passed, it gets queried as a slug and a 0 = 1 is added to the WHERE query.

Attached patch moves the taxonomy query to search by term_id instead.

#4 @jeremyfelt
6 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 3.9

27564.diff being that attached patch. :)

#5 @genkisan
6 years ago

Patch works for case 2. Thanks!

My bad for case 1. It was caused by the pre_get_posts action in my functions.php to exclude some categories.

function my_pre_get_posts($query) {
	$query->set('cat', '-4');
	return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');

So when the in_same_cat parameter is true, the prev/next links are not displayed (in the single post under that category). I got it to work with a remove_action:

remove_action('pre_get_posts', 'my_pre_get_posts');
previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ), true );
next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ), true );

Seems like the pre_get_posts action is applied in WP 3.9 though not in WP 3.8.1. Not sure if that's the intended behavior. If it is then I will just use the remove_action as above :)

#6 @nacin
6 years ago

You need to check $query->is_main_query() before doing pre_get_posts manipulations.

#7 @genkisan
6 years ago

I see. Thanks!

#8 @nacin
6 years ago

  • Milestone 3.9 deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Closing this as a duplicate of #26937 as we're reverting the adjacent post changes for 3.9.

Note: See TracTickets for help on using tickets.