WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#32833 closed defect (bug) (fixed)

previous_post_link/next_post_link disappear when excluding by post_format

Reported by: markshiz Owned by: boonebgorges
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.2.2
Component: General Keywords: has-patch needs-testing
Focuses: Cc:

Description

The following code gives me back a term_id from get_term_by, but causes the next/previous links to disappear:

<?php print_r(get_taxonomies()); ?>
<?php print_r(get_term_by('slug', 'post-format-aside', 'post_format')->term_id); ?>
<?php previous_post_link('&lsaquo; %link', '%title', false, array(get_term_by('slug', 'post-format-aside', 'post_format')->term_id )); ?>
<?php next_post_link('%link &rsaquo;', '%title', false, array(get_term_by('slug', 'post-format-aside', 'post_format')->term_id )); ?>

The object returned by get_term_by is:

STDCLASS OBJECT ( [TERM_ID] => 10 [NAME] => ASIDE [SLUG] => POST-FORMAT-ASIDE [TERM_GROUP] => 0 [TERM_TAXONOMY_ID] => 10 [TAXONOMY] => POST_FORMAT [DESCRIPTION] => [PARENT] => 0 [COUNT] => 0 [FILTER] => RAW )
UPDATE 2

When I enable the Wordpress query debugging, I see the following query being triggered, which is the empty set.

[mysql> SELECT P.ID FROM WP_POSTS AS P INNER JOIN WP_TERM_RELATIONSHIPS AS TR ON P.ID = TR.OBJECT_ID INNER JOIN WP_TERM_TAXONOMY TT ON TR.TERM_TAXONOMY_ID = TT.TERM_TAXONOMY_ID WHERE P.POST_DATE < '2015-06-01 20:10:00' AND P.POST_TYPE = 'PROJECT' AND TT.TAXONOMY = 'CATEGORY' AND P.ID NOT IN ( SELECT TR.OBJECT_ID FROM WP_TERM_RELATIONSHIPS TR LEFT JOIN WP_TERM_TAXONOMY TT ON (TR.TERM_TAXONOMY_ID = TT.TERM_TAXONOMY_ID) WHERE TT.TERM_ID IN (10) ) AND ( P.POST_STATUS = 'PUBLISH' OR P.POST_STATUS = 'PRIVATE' ) ORDER BY P.POST_DATE DESC ;]

Empty set (0.01 sec)

What's strange about the above is that the query appears to be attempting to find the previous post based on the category taxonomy, which I have explicitly asked the function not to do through the query parameters. This seems like a bug in Wordpress to me.

Attachments (1)

32833.diff (3.5 KB) - added by boonebgorges 5 years ago.

Download all attachments as: .zip

Change History (7)

#1 @boonebgorges
5 years ago

  • Keywords close reporter-feedback added

Hi markshiz -

Thanks for the ticket, and sorry for the delay in responding.

I'm not sure I totally understand what you think the incorrect behavior is.

What's strange about the above is that the query appears to be attempting to find the previous post based on the category taxonomy, which I have explicitly asked the function not to do through the query parameters.

How have you done this? Is it because you've passed false to the third param, $in_same_term? If so, note that this is about specific terms, not entire taxonomies.

next_post_link() and previous_post_link() each accept a fifth parameter $taxonomy. The default value of this parameter is 'category'. That's why you see a 'category' clause in the SQL query. So I don't think there's a bug here.

It's hard to say what's happening in the case of the "disappearing" links. Looking at the SQL and the arguments you're passing to the _post_link() functions, I'd say it's because there are no posts with 'post_type=project' on your installation that belong to any categories and do not belong to the 'post-format-aside' taxonomy. What is it that you *intend* to be doing with these links?

#3 @boonebgorges
5 years ago

  • Keywords has-patch needs-testing added; close reporter-feedback removed
  • Milestone changed from Awaiting Review to 4.4

Thanks for the link.

After some more digging, I managed to find a bug. I'm not 100% sure that this is what's causing your issue, but here goes.

In [29248], the clause that handles the $excluded_terms param in get_adjacent_post() was converted to a subquery of the form AND p.ID NOT IN ( SELECT ... ), where it used to be some sort of JOIN against the taxonomy tables. However, the $join and $where clauses were still being set up as if the taxonomy table were still involved in the $excluded_terms query.

More specifically, the query involved an INNER JOIN against the wp_term_relationships and wp_term_taxonomy tables. The INNER JOIN means that rows from the post table will be missed if they don't also have at least one row in the wp_term_relationships table (and, in most cases, one that matches the $taxonomy param). Usually, this is the case - posts created through the normal WP interface get the category 'Uncategorized'. But I'm betting that items in your 'project' post type don't have *any* rows in wp_term_relationships. As a result, they're not being included in the query.

Fix and unit test demonstrating the problem in [32833.diff]. markshiz, can you please patch your installation to see if this solves the problem for you?

@boonebgorges
5 years ago

#4 @wonderboymusic
5 years ago

  • Owner set to boonebgorges
  • Status changed from new to assigned

#5 @boonebgorges
5 years ago

Let's go with this, as it's clearly a bug one way or other. markshiz, if it doesn't fix your problem, feel free to leave more details.

#6 @boonebgorges
5 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 34088:

Remove extraneous table join in get_adjacent_post().

Since [29248], a table join has not been necessary to process the
$excluded_terms parameter of get_adjacent_post(). Aside from adding extra
overhead, this join meant that post records that don't have any corresponding
rows in wp_term_relationships were erroneously excluded from results.

Fixes #32833.

Note: See TracTickets for help on using tickets.