Opened 11 years ago
Closed 11 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: |
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)
Change History (9)
#2
@
11 years ago
- Milestone changed from 3.9 to Awaiting Review
Unconfirming this for a bit until I can confirm it again. :)
#3
@
11 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
@
11 years ago
- Keywords has-patch added
- Milestone changed from Awaiting Review to 3.9
27564.diff being that attached patch. :)
#5
@
11 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 :)
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]