Today’s lesson — WP_Query

I was trying to get a list of person-type posts (person being a custom post type). I had a chunk of code that looked a little like this:


$args = array( 'post_type'  => Class::POST_TYPE, 'fields'     => 'ids', 'nopaging'   => true, 'orderby'    => array( 'menu_order', 'title' ), 'order' => 'ASC', 'numberposts' => -1, ); $query = new WP_Query( $args );

But it just dumped them out using post_date ordering instead, like it was defying me. I couldn’t figure it out. I hammered away at it, trying different things. 'orderby' => array( 'menu_order' ) didn’t work, but 'orderby' => 'rand' did. I think I somehow managed to skip over trying 'orderby' => 'menu_order' — I suspect, in hindsight, that might have worked.

In the end I resorted to reading the documentation. Here’s the code snippet that worked:


$args = array( 'post_type' => Class::POST_TYPE, 'fields' => 'ids', 'nopaging' => true, 'orderby' => array( 'menu_order' => 'ASC', 'title' => 'ASC', ), 'numberposts' => -1, ); $query = new WP_Query( $args );

Apparently this new, more powerful orderby clause is new in WordPress version 4.0 and up. I like it, it works for me. Now that I’ve figured it out, anwyays…