Opened 12 years ago
Closed 10 years ago
#24597 closed enhancement (fixed)
get_{$adjacent}_post_sort filter should have post type parameter
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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)
Change History (10)
#1
in reply to:
↑ description
@
12 years ago
#2
@
12 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.
#3
@
12 years ago
- Component changed from General to Post Types
- Resolution set to wontfix
- Status changed from new to closed
#4
@
12 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.
Note: See
TracTickets for help on using
tickets.
Replying to helgatheviking:
This is not quite correct. The post type is, as always, a property of the
$postobject, as$post->post_type. This property is just passed as a parameter to the$wpdb->prepare()method, which result is then passed inapply_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_sortthen it would be logical to also pass it for the other two filters (get_{$adjacent}_post_whereandget_{$adjacent}_post_join).I really can't see the need for this extra parameter.