2

How to order posts by alphabet with numbers at the end.

  $params = array(
    'limit' => -1,
    'order' => 'ASC',
  );

I have a list of businesses (Pods framework post type) and they are in ordered by name. Some business names have a number in them, such as 7-11 and the client wants those to be at the end, so a->z->0->9 I guess. How would I go about this?

Actual

7-11,
Apple store,
Zebra store

Desired

Apple store,
Zebra store,
7-11
2

1 Answer 1

0

With some help of this answer and this post I managed to get your desired sorting hooking into the posts_orderby filter.

You would need to add some additional check so this only affects your post type and on your page of choice but this snippet gives you the required statement to get the

Apple store
Zebra store
7-11

posts sorting, provided their slug is

apple-store
zebra-store
7-11

since I order by post_name which is the post's slug

add_filter( 'posts_orderby' , 'wpse_custom_orderby_statement' );
function wpse_custom_orderby_statement( $orderby ) {

  $orderby = " post_name+0<>0 ASC, post_name+0, post_name";

  return $orderby;
}

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.