Skip to content

Commit

Permalink
Merge pull request #108 from 10up/revert/103
Browse files Browse the repository at this point in the history
Add back the check for hierarchical post types, instead of just relying on `page-attributes`
  • Loading branch information
dkotter authored Oct 28, 2022
2 parents 820a024 + afee921 commit 4d4fd43
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Yep. When you register the post type, include the `page-attributes` feature in t

`'supports' => array( 'title', 'editor', 'page-attributes' ),`

Alternatively, when you register the post type, set `hierarchical` to `true` - hierarchical post types natively order by menu order.

You can also take advantage of the `simple_page_ordering_is_sortable` filter, which passes the result of the default check and the post type name, to override default behavior.

### I want my non-hierarchical post type to be sortable. Help!
Expand All @@ -60,11 +62,11 @@ Where 5 is the number of items to batch on each request (the default is 50). Not

This feature is already built into WordPress natively, but a bit tucked away. If you pull down the "Screen Options" tab up top (on the list of post objects) there's a field where you can specify the number of items to show per page. I decided it was not a very good practice to duplicate this.

### How can I exclude certain custom post types?
### How can I modify sortable post types?

Custom post types can be excluded by using the `simple_page_ordering_is_sortable` filter.
Post types can be included or excluded by using the `simple_page_ordering_is_sortable` filter.

For example, with `excluded_post_type` as the custom post type ID, add the following snippet in the theme function file or custom plugin:
For example, to exclude the `excluded_post_type` custom post type, add the following snippet in the theme function file or custom plugin:

```
add_filter( 'simple_page_ordering_is_sortable', function( $sortable, $post_type ) {
Expand All @@ -75,6 +77,17 @@ add_filter( 'simple_page_ordering_is_sortable', function( $sortable, $post_type
}, 10, 2 );
```

To include the `include_post_type` custom post type, add the following snippet in the theme function file or custom plugin:

```
add_filter( 'simple_page_ordering_is_sortable', function( $sortable, $post_type ) {
if ( 'include_post_type' === $post_type ) {
return true;
}
return $sortable;
}, 10, 2 );
```

### Can I use REST to order posts?

Yes. The plugin registers the REST endpoint `simple-page-ordering/v1/page_ordering`.
Expand Down
19 changes: 16 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Yep. When you register the post type, include the `page-attributes` feature in t

`'supports' => array( 'title', 'editor', 'page-attributes' ),`

Alternatively, when you register the post type, set `hierarchical` to `true` - hierarchical post types natively order by menu order.

You can also take advantage of the `simple_page_ordering_is_sortable` filter, which passes the result of the default check and the post type name, to override default behavior.

= I want my non-hierarchical post type to be sortable. Help! =
Expand All @@ -70,11 +72,11 @@ Where 5 is the number of items to batch on each request (the default is 50). Not

This feature is already built into WordPress natively, but a bit tucked away. If you pull down the "Screen Options" tab up top (on the list of post objects) there's a field where you can specify the number of items to show per page. I decided it was not a very good practice to duplicate this.

= How can I exclude certain custom post types? =
= How can I modify sortable post types? =

Custom post types can be excluded by using the `simple_page_ordering_is_sortable` filter.
Post types can be included or excluded by using the `simple_page_ordering_is_sortable` filter.

For example, with `excluded_post_type` as the custom post type ID, add the following snippet in the theme function file or custom plugin:
For example, to exclude the `excluded_post_type` custom post type, add the following snippet in the theme function file or custom plugin:

`
add_filter( 'simple_page_ordering_is_sortable', function( $sortable, $post_type ) {
Expand All @@ -85,6 +87,17 @@ add_filter( 'simple_page_ordering_is_sortable', function( $sortable, $post_type
}, 10, 2 );
`

To include the `include_post_type` custom post type, add the following snippet in the theme function file or custom plugin:

```
add_filter( 'simple_page_ordering_is_sortable', function( $sortable, $post_type ) {
if ( 'include_post_type' === $post_type ) {
return true;
}
return $sortable;
}, 10, 2 );
```

= Can I use REST to order posts? =

Yes. The plugin registers the REST endpoint `simple-page-ordering/v1/page_ordering`.
Expand Down
15 changes: 12 additions & 3 deletions simple-page-ordering.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ public static function load_textdomain() {
* @return boolean
*/
private static function is_post_type_sortable( $post_type = 'post' ) {
return apply_filters( 'simple_page_ordering_is_sortable', post_type_supports( $post_type, 'page-attributes' ), $post_type );
$sortable = ( post_type_supports( $post_type, 'page-attributes' ) || is_post_type_hierarchical( $post_type ) );

/**
* Change default ordering support for a post type.
*
* @since 2.2.4
*
* @param boolean $sortable Whether this post type is sortable or not.
* @param string $post_type The post type being checked.
*/
return apply_filters( 'simple_page_ordering_is_sortable', $sortable, $post_type );
}

/**
Expand Down Expand Up @@ -416,8 +426,7 @@ public static function page_ordering( $post_id, $previd, $nextid, $start, $exclu
public static function sort_by_order_link( $views ) {
$class = ( get_query_var( 'orderby' ) === 'menu_order title' ) ? 'current' : '';
$query_string = remove_query_arg( array( 'orderby', 'order' ) );
$sortable = self::is_post_type_sortable( get_post_type() );
if ( $sortable ) {
if ( ! is_post_type_hierarchical( get_post_type() ) ) {
$query_string = add_query_arg( 'orderby', 'menu_order title', $query_string );
$query_string = add_query_arg( 'order', 'asc', $query_string );
}
Expand Down

0 comments on commit 4d4fd43

Please sign in to comment.