0

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">';
    the_title();
    echo '</h2>';
    the_excerpt();
    echo  '</div>';
endwhile;
2

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.

http://codex.wordpress.org/Class_Reference/WP_Query

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',
  'order'=>'ASC' 
);
| improve this answer | |
  • If this solved the question please mark it as "Accepted". Look for the check mark on the left. – s_ha_dum Jun 24 '13 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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