Support » Plugins » Hacks » Page ordering not working when using tax_query

  • Resolved webheadllc


    On the admin Pages screen (edit.php) I’m adding the following filter to only show a certain taxonomy for certain users.

    The problem is this doesn’t list out the pages in the same order as the default page ordering. For example this is how the pages show without my taxonomy filter:

    • Parent1 (menu_order: 4)
      • Child 1-1 (menu_order:0)
    • Parent2 (menu_order: 5)
      • Child 2-1 (menu_order:0)
      • Child 2-1 (menu_order:1)

    And this is how the pages show with my taxonomy filter:

      • Child 1-1 (menu_order:0)
      • Child 2-1 (menu_order:0)
      • Child 2-1 (menu_order:1)
    • Parent1 (menu_order: 4)
    • Parent2 (menu_order: 5)

    Understandably it is ordering by menu_order but it is ignoring the hierarchy. This different ordering only occurs when I set the tax_query. Is there any way to set the tax_query but still have it ordered as in the first example?

    I have simplified my code to the following:

    if ( $pagenow == 'edit.php' )
            add_filter( 'pre_get_posts', 'my_posts_query' );
        function my_posts_query( $query ){
            $taxquery = array(
                    'taxonomy' => $taxonomy,
                    'field' => 'id',
                    'terms' => $ids
            $query->set( 'tax_query', $taxquery );
            $query->set( 'orderby', 'menu_order title' );
            $query->set( 'order', 'ASC' );</li>
            return $query;

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


    This is strange. I cannot replicate your results, I get a hierarchical list using your filter. The only thing I changed was the values for $taxonomy and $ids to reflect my installation. I assigned similar menu order values to my pages, my pages have similar children.

    I have to think your theme or a plugin is corrupting the hierarchical_display property of the table between when it’s prepared and when it’s displayed. Why only when you use your filter I cannot say.

    My installation is pretty basic with few plugins and a child theme of twentyeleven. I can’t offer a solution, but thought the fact it works for me might be useful somehow.

    Thanks for trying it. I guess I should’ve tried it on a clean install before posting. I have a pretty basic theme and very little other plugins so I didn’t think of it, but you never know. i’ll post back here if i find anything.


    I think I have found the problem. My “parent” pages aren’t at the top level. I looked in wp-admin/includes/class-wp-posts-list-table.php and found that the top level pages are determined when $page->post_parent == 0. Since my “parent” pages aren’t top level they aren’t considered included in $top_level_pages. bummer.

    I guess my only solution is to somehow include the top level pages even if they don’t have the taxonomy I want.

    Moderator bcworkz


    Ah! That explains it. I had assumed your parents were top level, so my attempt to model the situation was all wrong.

    I have one idea for a hack, but it would require you to be able to identify the correct not-top-level parent posts in the array returned by WP_Query. This could be easy or impossible depending on your situation. If that can happen, perhaps you could alter the associated parent_post values in that array to all be zero. The query results are in the global $wp_query->posts. Finding a good hook to do this from is sort of an issue. I found ‘bulk_post_updated_messages’ which appears to fire at the right time. It obviously has nothing to do with query results, but since you’re acting on a global, it doesn’t really matter what, only when.

    It’s a long shot if this would work at all, and dependent on being able to identify parent posts to change. Perhaps it’s still worth a closer look.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Page ordering not working when using tax_query’ is closed to new replies.