• Using the following code to pull custom posts:

    query_posts(array(‘paged’ => $paged, ‘posts_per_page’ => 9, ‘post_type’ => ‘gallery’, ‘cat’ => $catid, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’));

    Unfortunately, this does not work properly if the order hasn’t been set (default is 0). Is there a way to push posts ordered 0 to the back of the list, or possibly change the default order value to something else (maybe 10000 or some other arbitrarily large number)?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can use a filter to do this. Add this function to your functions.php:

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');

    Then set the filter just before your query and clear it just after, similar to this:

    $mam_global_orderby = " if($wpdb->posts.menu_order = 0, 9999, $wpdb->posts.menu_order) ASC, UPPER($wpdb->posts.post_title) ASC";
    query_posts($args); // Your query goes here
    $mam_global_orderby = ''; // Remove the filter

    An easier option could be to just change the order from ASC to DESC.

    query_posts(array('paged' => $paged, 'posts_per_page' => 9, 'post_type' => 'gallery', 'cat' => $catid, 'orderby' => 'menu_order', 'order' => 'DESC'));

    That will display the highest menu order # first and go down from there.

    I believe the poster wanted the order to be ascending except for zero. So the order would be ‘2,3,4,7,9,0’, not ‘9,7,4,3,2,0’. If I am wrong, then your method should work.

    I was going off the “way to push posts ordered 0 to the back of the list” part. I didn’t even consider the ascending except for zero part. I wouldn’t have figured that out as elegantly as you did so I’m glad you got that one 🙂

    Either way, I hope they got what they needed!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘orderby => menu_order for values above 0’ is closed to new replies.