Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Vivek

Sorting not working on Custom Post Type

Hey guys,

I have a custom post type called “booking”, with custom post statuses:

  • Upcoming
  • Past
  • Cancelled
  • Late Cancelled
  • Charged Cancelled
  • No Show

The problem
When viewing the Bookings screen, the post status is set to “All” on default. In classes/sortable/classes/model.php:308 the get_posts method assumes default status to be array('any', 'trash'). Any doesn’t translate to my list of custom post statuses.

Here is what I did to fix it:

public function get_posts( $args = array() ) {
$default_post_status = $this->storage_model->get_post_type() == 'booking'
? array( 'upcoming', 'past', 'cancelled', 'late-cancelled', 'charged-cancelled', 'no-show' )
: array( 'any', 'trash' );

$defaults = array(
'posts_per_page' => - 1,
'post_status' => $default_post_status,
'post_type' => $this->storage_model->get_post_type(),
'fields' => 'ids',
'no_found_rows' => 1, // lowers our carbon footprint
);

$post_ids = (array) get_posts( array_merge( $defaults, $args ) );

return $post_ids;
}

It would be nice if you had a filter called “default_sorting_post_status”, and I can hook into it rather than changing core.

Thanks!

10 years ago
Tobias Schutter
Developer

Since WordPress doesn’t allow the registering of custom status per post type, we will have to do a workaround like you suggested.

I will add a filter for this.


public function get_posts( $args = array() ) {
	$defaults = array(
		'posts_per_page' => -1,
		'post_status'    => apply_filters( 'cac/addon/sortable/post_status', array( 'any', 'trash' ), $this ),
		'post_type'      => $this->storage_model->get_post_type(),
		'fields'         => 'ids',
		'no_found_rows'  => 1,
	);

	return (array) get_posts( array_merge( $defaults, $args ) );
}
10 years ago

You must be logged in to reply to this topic.