Ticket #4822 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

get_previous_post_join and get_next_post_join cannot be used in certain cases

Reported by: Cimmo Assigned to: ryan
Priority: normal Milestone: 2.3
Component: General Version: 2.2.2
Severity: normal Keywords: dev-reviewed
Cc:

Description

get_previous_post_join and get_next_post_join cannot be used in some cases because if someone add a INNER JOIN with another table that has the ID column too then SQL return the error:

WordPress database error: [Column 'ID' in field list is ambiguous] SELECT ID, post_title FROM wp_posts INNER JOIN wp_a51_posts AS a51_posts ON wp_posts.ID=a51_posts.POST_ID WHERE post_date < '2007-07-12 12:31:13' AND post_type = 'post' AND post_status = 'publish' AND a51_posts.LEVEL<=-1 ORDER BY post_date DESC LIMIT 1

I suggest to change these rows in get_previous_post function in link-template.php:

line 325: $join = " INNER JOIN $wpdb->post2cat ON p.ID= $wpdb->post2cat.post_id ";

line 346: $where = apply_filters( 'get_previous_post_where', "WHERE p.post_date < '$current_post_date' AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $in_same_cat, $excluded_categories );

line 347: $sort = apply_filters( 'get_previous_post_sort', 'ORDER BY p.post_date DESC LIMIT 1' );

line 349: return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort");

similar can be done into get_next_post function: lines: 362, 383, 384, 386

Attachments

link-template.php.diff (2.4 kB) - added by Cimmo on 08/31/07 11:52:30.
Patch against 2.3 beta1, check it please
4822.diff (3.6 kB) - added by westi on 08/31/07 11:59:30.
Patch for svn diff so trac understands it and it has context

Change History

08/27/07 14:51:16 changed by Cimmo

  • milestone changed from 2.4 (next) to 2.3.

08/27/07 14:54:13 changed by Cimmo

  • version set to 2.2.2.

08/31/07 11:52:30 changed by Cimmo

  • attachment link-template.php.diff added.

Patch against 2.3 beta1, check it please

08/31/07 11:59:30 changed by westi

  • attachment 4822.diff added.

Patch for svn diff so trac understands it and it has context

08/31/07 12:12:05 changed by westi

  • owner changed from anonymous to ryan.

Patch looks interesting.

Ryan can you give feedback on this - my SQLfu is not upto it today ;-)

09/03/07 16:34:32 changed by ryan

  • keywords set to dev-reviewed.

Looks good.

09/03/07 16:46:32 changed by westi

  • status changed from new to closed.
  • resolution set to fixed.

(In [6019]) Change queries in get_{next|previous}_post so that plugins can INNER JOIN on other tables with an ID column. Fixes #4822 props Cimmo.