• Resolved fawp

    (@fawp)


    Hi, I have created a hierarchical CPT and I expected that, with a query, there would be a way to show the posts’ hierarchy.

    For example, if I had some continents and countries under Earth I would expect the following order to be shown:

    Earth – Level 1
    Africa – Level 2
    Nigeria – Level 3
    Zaire – Level 3
    North America – Level 2
    United States – Level 3
    Canada – Level 3
    Europe – Level 2
    Germany – Level 3
    Greece – Level 3
    Spain – Level 3

    However, the order displayed is

    Germany – Level 3
    Greece – Level 3
    Spain – Level 3
    Canada – Level 3
    United States – Level 3
    Nigeria – Level 3
    Zaire – Level 3
    Africa – Level 2
    North America – Level 2
    Europe – Level 2
    Earth – Level 1

    In my WP_Query I have tried both parent and menu_order for the orderby parameter but neither is producing the expected result.

    This appears to be the opposite problem to the one posted in this thread

    https://www.ads-software.com/support/topic/hierarchy-post-type-post-table-custom-sort/

    @bcworkz if you read this, perhaps you can shed some light?

    Thank you
    fawp

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

    (@bcworkz)

    Hi fawp,

    That sort of ordering is beyond the capabilities of SQL ordering. The way hierarchy is organized is not conducive to SQL ordering. You basically need to “walk” the hierarchy tree in order to generate the desired structured output.

    You can look at walk_page_tree() to see how WP does this to list hierarchical pages. Sadly, there doesn’t seem to be a way to pass CPTs to this instead of pages. You’ll probably need to extend the Walker class yourself just like WP extends it for its Walker_Page class.

    The good part of all this is you can control the HTML output so not only are posts listed in the desired order, you can indent and otherwise style children to make the hierarchy even more clear to visitors.

    FYI: You shouldn’t @ reference other members in topic posts, the practice is frowned upon here. It’s tolerated if a member is already involved in the topic through a reply, though doing so is unnecessary. It’s a common forum practice in general, so tolerated when one is already involved. Members can chose for themselves if they want notifications. Forcing notifications without consent could be seen as spamming by some.

    You’ll see moderators @ referencing members because it’s important they are notified about a moderation action. Any other @ referencing is really unnecessary.

    The concept of support forums is to get help from anyone who may be able to help. By @ referencing a specific member, you are discouraging others from helping you who may be able to better help you. They see the @ reference and think “Ehh, why bother answering, bcworkz will get it.” I personally am not bothered by @ references because I tend to ignore the volume of notifications I get. I have a different workflow to ensure I see replies in topics I’ve participated in.

    Thread Starter fawp

    (@fawp)

    Many thanks for your answer and apologies about the breach of netiquette.

    It may be that I misunderstood my request to be similar to that in the topic https://www.ads-software.com/support/topic/hierarchy-post-type-post-table-custom-sort/ – I do see in that topic that a reference is made to “$hierarchical_display is set for any hierarchical post type and it essentially forces children to be listed under their parent”. I understand that property belongs to the WP_Posts_List_Table class.

    Does that mean that through WP_Posts_List_Table hierarchical CPTs are shown hierarchically, but attempting this via a WP_Query won’t work because of what bcworkz mentioned?

    Thanks again
    fawp

    Moderator bcworkz

    (@bcworkz)

    No worries about @ references, you didn’t know. Now you know ??

    Right, WP_Query relies upon SQL sorting, which cannot sort hierarchically because of how WP hierarchy is defined. “Under the hood,” the page list table roughly follows the same approach as Walker_Page class. It takes a list of pages and sorts them hierarchically. The list is passed on to display functions. See _display_rows_hierarchical() if you’re curious. Compare to Walker::walk().

    If you are generating output for hierarchical front end display, I think walk_page_tree() and Walker_Page is easier to use as a guide for your own effort because it’s not intermixed with other list table functions. Either option will work. They essentially use the same sorting logic, they diverge only in how the output is accomplished.

    Thread Starter fawp

    (@fawp)

    Great, thank you very much for your help bcworkz!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome.

    Hey, I just noticed that get_pages() supports any post type and hierarchical listing!

    I was just looking at wp_list_pages() which seemed to only support pages. On closer inspection, I realized it takes a list of IDs independent of post type. That caused me to look at what uses that function, leading me to get_pages().

    Sorry for leading you astray earlier.

    Thread Starter fawp

    (@fawp)

    Excellent! I’ve just tried get_pages() and it works. Thank you so much for taking the time to post this, bcworkz! ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hierarchical Custom Post Type sort not working’ is closed to new replies.