Support » Fixing WordPress » Default sorting posts of custom post types with the `hierarchical`

  • Hi there,

    Custom post types with the hierarchical argument value set to true list posts in the order that the older ones come first in the admin post listing table.

    To reproduce the problem,

    1. Create a test custom post type with the below code.

    
    new Tests_CustomPostType;
    
    class Tests_CustomPostType{
    
        public function __construct() {
            add_filter( 'init', array( $this, 'testPostType' ) );
        }
        public function testPostType() {
    
            $labels = array(
                'name'                  => _x( 'Books', 'Post type general name', 'textdomain' ),
                'singular_name'         => _x( 'Book', 'Post type singular name', 'textdomain' ),
                'menu_name'             => _x( 'Books', 'Admin Menu text', 'textdomain' ),
            );
    
            $args = array(
                'labels'             => $labels,
                'public'             => true,
                'publicly_queryable' => true,
                'show_ui'            => true,
                'show_in_menu'       => true,
                'query_var'          => true,
                'rewrite'            => array( 'slug' => 'book' ),
                'capability_type'    => 'post',
                'has_archive'        => true,
                'hierarchical'       => true,
                'menu_position'      => null,
                'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
            );
    
            register_post_type( 'book', $args );
        }
    
    }
    

    2. Create several posts of that book post type.
    3. Go to Dashboard -> Books.
    4. You’ll see the posts listed in the reverse date order.

    I would suggest sorting posts normally as it looks inconsistent.

    • This topic was modified 1 year, 9 months ago by umchal.
Viewing 4 replies - 1 through 4 (of 4 total)
  • I would suggest sorting posts normally as it looks inconsistent.

    What is “normal”?
    All lists of posts are sorted by date, descending, by default. Pages (which are hierarchical) are sorted by menu_order and then date.

    You can supply a filter to affect the sort parameter prior to the query. That is how a plugins like https://wordpress.org/plugins/search/sort+posts/ work.

    Thread Starter umchal

    (@umchal)

    What is “normal”?

    To list posts by the order they are created. Even pages are sorted in that manner.

    Well, “normal” for a web log is to list posts in date descending order, so that you see the latest one at the top.

    Thread Starter umchal

    (@umchal)

    Ops, I said the opposite. You’re right: to list posts in the order that latest ones come first.

    So we both agree with that point about what “normal” is.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Default sorting posts of custom post types with the `hierarchical`’ is closed to new replies.