I am trying to order a custom post type called "Teachers" by a field called sort_by. I am not sure why this isn't working.

Here is my code:

$args = array( 'post_type' => 'teacher', 'posts_per_page' => 30, 'order_by' => 'sort_by' ,'order'=>'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    echo '<div class="entry-content"><h2 class="teachers">';
    echo '</h2>';
    echo  '</div>';

1 Answer 1


The argument is orderby not order_by but I am not sure what sort_by is. If that is a custom meta field you need to alter the query.

'meta_value' - Note that a 'meta_key=keyname' must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use 'meta_value_num' instead for numeric values.


You need to pass one argument for the meta_key and another for the orderby.

$args = array( 
  'post_type' => 'teacher',
  'posts_per_page' => 30,
  'meta_key' => 'sort_by',
  'orderby' => 'meta_value',
  • If this solved the question please mark it as "Accepted". Look for the check mark on the left.
    – s_ha_dum
    Commented Jun 24, 2013 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.