Make WordPress Core

Opened 10 years ago

Closed 8 years ago

#24597 closed enhancement (fixed)

get_{$adjacent}_post_sort filter should have post type parameter

Reported by: helgatheviking's profile helgatheviking Owned by: chriscct7's profile chriscct7
Milestone: 4.4 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch needs-refresh
Focuses: Cc:

Description

In case a user doesn't want to change the sort order for every post type, the post type should be passed as an extra parameter. The post type is already a variable is already passed in the preceding post_where filter.

$sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );

should be updated with:

$sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post->post_type );

in wp-includes/link-template.php

Attachments (2)

24597.diff (1.3 KB) - added by JustinSainton 10 years ago.
24597.1.diff (1.3 KB) - added by JustinSainton 10 years ago.

Download all attachments as: .zip

Change History (10)

#1 in reply to: ↑ description @knutsp
10 years ago

Replying to helgatheviking:

The post type is already a variable is already passed in the preceding post_where filter.

This is not quite correct. The post type is, as always, a property of the $post object, as $post->post_type. This property is just passed as a parameter to the $wpdb->prepare() method, which result is then passed in apply_filters() call.

I see no need for passing a property of a global object to a callback function, since the callback function, in this case may use get_post_type(), or, generally, use it directly by declaring the global object. Like I do in my child themes for sorting post type 'foo' by post title:

function knutsp_previous_post_sort( $sort ) {
	if ( 'foo' === get_post_type() )
		return "ORDER BY p.post_title DESC LIMIT 1";
	else
		return $sort;
}
add_filter( 'get_previous_post_sort', 'knutsp_previous_post_sort' );

or like this:

function knutsp_previous_post_where( $where ) {
	global $wpdb, $post;
	if ( 'foo' === $post->post_type )
		return $wpdb->prepare( "WHERE p.post_title < '%s' AND p.post_type = 'foo' AND p.post_status = 'publish'", $post->post_title );
	else
		return $where;
}
add_filter( 'get_previous_post_where', 'knutsp_previous_post_where' );

If this is needed for get_{$adjacent}_post_sort then it would be logical to also pass it for the other two filters (get_{$adjacent}_post_whereand get_{$adjacent}_post_join).

I really can't see the need for this extra parameter.

#2 @helgatheviking
10 years ago

My bad I guess. It looked like the post type was being passed. I appreciate the complete explanation... and would move to close this ticket.

Last edited 10 years ago by helgatheviking (previous) (diff)

#3 @knutsp
10 years ago

  • Component changed from General to Post Types
  • Resolution set to wontfix
  • Status changed from new to closed

#4 @nacin
10 years ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened

In general it is best to not rely on global scope. Passing $post to this filter makes a lot of sense. $post should probably be passed to all of these filters.

@JustinSainton
10 years ago

#5 @JustinSainton
10 years ago

  • Keywords has-patch added

#6 @JustinSainton
10 years ago

Refreshed patch. Looks good to me.

#7 @chriscct7
8 years ago

  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to 4.4
  • Owner set to chriscct7
  • Status changed from reopened to assigned

#8 @wonderboymusic
8 years ago

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

In 33968:

Add a parameter, $post, to get_{$adjacent}_post_join, get_{$adjacent}_post_where, and get_{$adjacent}_post_sort

Props JustinSainton.
Fixes #24597.

Note: See TracTickets for help on using tickets.