WordPress.org

Make WordPress Core

Opened 10 months ago

Last modified 9 months ago

#24597 reopened enhancement

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

Reported by: helgatheviking Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
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 months ago.
24597.1.diff (1.3 KB) - added by JustinSainton 9 months ago.

Download all attachments as: .zip

Change History (8)

comment:1 in reply to: ↑ description knutsp10 months 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.

comment:2 helgatheviking10 months ago

My bad I guess. It looked like the post type was being passed. I appreciate the complete explanation...

Version 0, edited 10 months ago by helgatheviking (next)

comment:3 knutsp10 months ago

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

comment:4 nacin10 months 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.

JustinSainton10 months ago

comment:5 JustinSainton10 months ago

  • Keywords has-patch added

JustinSainton9 months ago

comment:6 JustinSainton9 months ago

Refreshed patch. Looks good to me.

Note: See TracTickets for help on using tickets.