WordPress.org

Ready to get started?Download WordPress

Forums

I cannot get page ordering in edit-pages.php to work (7 posts)

  1. torfason
    Member
    Posted 6 years ago #

    I am using WordPress as a combined Blog/CMS, and my site has quite a number of pages, that should logically be ordered according to menu_order.

    I've set the values and they do show up in the correct order on the public site, but in the admin environment, on edit-pages.php, they will not show up in the correct order, no matter how I try. They seem to be ordered by ID instead.

    I've read the threads on this on the forum, namely
    http://wordpress.org/support/topic/101915
    http://wordpress.org/support/topic/123139

    I am also aware of the quuery string that should determine this order:
    $query_str = "post_type=page&orderby=menu_order title&what_to_show=posts$post_status_q&posts_per_page=-1&posts_per_archive_page=-1&order=asc";

    I've even tried changing order=asc to order=desc, and that has the expected effect. However, no matter how I change the orderby variable within the query string, edit-pages adamantly refused to order them correctly.

    I've installed the mass-edit-pages plugin, which is great for many reasons. The plugin sets the SQL directly, and that works fine (page list ordered according to menu_order).

    I hope this is some small thing I am overlooking, and would appreciate any assistance.

  2. TrishaM
    Member
    Posted 6 years ago #

    Can you please post exactly what you do have in your edit-pages.php file?

    It seems like since this was solved in post topic 101915 that you linked to it must be some small thing that perhaps another pair of eyes on it could catch....

  3. torfason
    Member
    Posted 6 years ago #

    Yes, 101915 seems to be a resolution, but my edit page actually contained a query string that looked as it should take care of this correctly, sorting first by menu_order and then by title. Note that I am not trying to alter the ordering, simply getting it to work the way it seems to be intended to work (it seems that menu_order is supposed to be the default sort column).

    (by the way, I am using version 2.5).

    Anyway, the code (which I don't think is modified from the original wordpress installation) is:

    $post_status_label = __('Manage Pages');
    $post_status_q = '';
    if ( isset($_GET['post_status']) && in_array( $_GET['post_status'], array_keys($post_stati) ) ) {
    	$post_status_label = $post_stati[$_GET['post_status']][1];
    	$post_status_q = '&post_status=' . $_GET['post_status'];
    	$post_status_q .= '&perm=readable';
    }
    
    $query_str = "post_type=page&orderby=menu_order title&what_to_show=posts$post_status_q&posts_per_page=-1&posts_per_archive_page=-1&order=asc";
    
    $query_str = apply_filters('manage_pages_query', $query_str);
    wp($query_str);
    
    if ( is_singular() )
    	wp_enqueue_script( 'admin-comments' );
    require_once('admin-header.php');

    I would have thought this would be correct, but it doesn't.

  4. TrishaM
    Member
    Posted 6 years ago #

    aha........2.5.......I believe that there is a typo on line 54 which is the query.

    If you change it just slightly to remove the 'menu_order ' (yes there is a blank space that should come out too).....

    `$query_str = "post_type=page&orderby=title&what_to_show=posts$post_status_q&posts_per_page=-1&posts_per_archive_page=-1&order=asc";

    It will order pages by name........

  5. TrishaM
    Member
    Posted 6 years ago #

    In case anyone else is looking to do this, be advised that if you change CORE FILES like this one you'll need to make this change anytime you update to a newer version of WP, so be sure to keep good notes on what to change and where. I have a notepad doc of all the core file changes I've made and why so that each time I upgrade I can go back and make those changes again (it really only takes me about 5 to 10 minutes total)....

    If you're not comfortable changing core files, try a plugin like My Page Order which will let you re-order your pages for your navigational structure and it also displays the pages in the same order in your Admin panel.......

  6. torfason
    Member
    Posted 6 years ago #

    Hi TrishaM, thank you so much for the help. I installed the My Page Order, which is actually great as well. I also tried your suggested edit and after making that change the pages are indeed ordered by the TITLE column.

    However, I see now that I was not clear enough in my post. My desired behavior IS actually to get the ordered by the MENU_ORDER column. I would have expected that the default setting for the orderby variable would accomplish this, since it is (in a completely fresh installation):
    orderby=menu_order title

    However, in my completely fresh installation - without any edits to the source files, my pages show up ordered by the ID column instead.

    I have now gone deeper and deeper into the core files (way deeper than I would prefer), and I ended up in query.php, where I found the following code on line 1250:

    for ($i = 0; $i < count($orderby_array); $i++) {
    				// Only allow certain values for safety
    				$orderby = $orderby_array[$i];
    				switch ($orderby) {
    					case 'menu_order':
    					case 'ID':
    						$orderby = "$wpdb->posts.ID";
    						break;
    					case 'rand':
    						$orderby = 'RAND()';
    						break;
    					default:
    						$orderby = "$wpdb->posts.post_" . $orderby;
    				}

    I know that omitting a break statement within a switch conditoion is generally not a good thing to do, so I decided to add one line to this code. After adding one line containing a break statement, the snippet now looks as follows:

    switch ($orderby) {
    					case 'menu_order':
    						break;
    					case 'ID':
    						$orderby = "$wpdb->posts.ID";
    						break;
    					case 'rand':
    						$orderby = 'RAND()';
    						break;
    					default:
    						$orderby = "$wpdb->posts.post_" . $orderby;
    				}

    And now the sort statement in edit-pages.php works correctly, sorting by the MENU_ORDER column. However, I am not sure why this fixes it or why the break statement was missing.

    So, my problem is resolved, kind of. Nevertheless, as you mention, it is bad to run an installation with modified core files, and in this case I worry that there is an actual reason for omitting the break statement. So I am wondering if this could be a bug, and if I should file a bug report?

  7. TrishaM
    Member
    Posted 6 years ago #

    I believe it is probably a bug that should be reported. It definitely looks like, after a very long night with too much caffeine, someone just accidentally left out the required 'break' statement.

    If it turns out to really be a typo that gets fixed in the next version (2.5.1) then you won't have to worry about needing to add it again :D

    Congratulations on being so persistent! And for being considerate enough to post your fix - whether or not it turns out to be a bug it will no doubt help someone else who has the same problem.

Topic Closed

This topic has been closed to new replies.

About this Topic