• Hi all,

    I have a custom post type which is hierarchical. It’s in relation to a sporting competition. I want the table to be sorted by date, and not the default wordpress order.

    For Example, his maybe wordpress default order.

    Parent 1 – start date 2010
    — P1 child 1 – start date 2011
    — P1 child 2 – start date 2012
    — P1 child 3 – start date 2013

    Parent 2 – stat date 2005
    — P2 child 1 – start date 2006
    — P2 Child 2 – start date 2012
    — P2 child 3 – start date 2014

    How do i get it to sort and show like this:

    — P2 child 3 – start date 2014
    — P1 child 3 – start date 2013
    — P2 Child 2 – start date 2012
    — P1 child 2 – start date 2012
    — P1 child 1 – start date 2011
    Parent 1 – start date 2010
    — P2 child 1 – start date 2006
    Parent 2 – stat date 2005

    I want the newest competition to always be on top, not the newest parent competition. It means that users will have to do search terms and go to the second page to find the latest competition if its parent is older.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t see any easy way to cleanly do that. The problem is the protected property $hierarchical_display is set for any hierarchical post type and it essentially forces children to be listed under their parent. There does not seem to be any way to access this property and change it in the normal post list table display process.

    In order to access it, you’d need to create your own custom post list table class by extending the default one. Then make your own admin page to display it. You could then remove the default admin menu page to avoid confusion. When you have your own class and admin page, you can display things in any manner you desire, but it’s a large departure from the default process.

    Thread Starter jenry

    (@jenry)

    yikes, i figured that. Funny enough, when i use the “post_clauses” filter, sortable columns when clicked, ends up sorting it the way i want it. No such luck on the default order though.

    Moderator bcworkz

    (@bcworkz)

    Yeah, when you do that, a different URL is requested with &order=asc added on. This causes the the whole $hierarchical_display bit to not be used. A quick hacky fix would be to alter the global $menu array, adding &orderby=date&order=desc to the default CPT URLs. You can do this in a callback hooked to the “admin_menu” action.

    The entire extended class replacement process would be the “formal, proper” approach, but is clearly ridiculous. In certain cases there’s little downside to a hacky approach. The only downside I can imagine in directly altering $menu is if the structure should be altered in core, your hack could become broken. Oh, the humanity! ??

    • This reply was modified 7 years, 5 months ago by bcworkz. Reason: wrong action named
    Thread Starter jenry

    (@jenry)

    A quick hacky fix would be to alter the global $menu array, adding &orderby=date&order=desc to the default CPT URLs.

    Thanks for that my brother, this is awesome. I don’t think i’ll need the hook. The custom post type is accessed as a submenu through the plugin’s dedicated menu. So i’ll alter the submenu slug from the normal edit.php?post_type=competitions and append orderby & order query.

    Thanks again. This is why I love being a developer. People always willing to help.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! The extra help is really great when it results in collaborative ideas for unusual approaches that we may not have thought of in isolation.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hierarchy Post Type Post Table Custom Sort’ is closed to new replies.