After the Long time, I come with the quick post that is how we sort data in WordPress by custom field.Today I wanted to sort by a custom field for company name menu in sidebar so its display category alphabetically in ascending order by custom field and it’s too simple to sort post, page or category by custom field in WordPress so let’s take a look!
For that I have used get_posts() function but you can use following code with query_post(),have_post() or any function which is used for loop.
$args = array( 'meta_key' => 'company', 'orderby' => 'meta_value', 'order' => 'ASC' ); $posts = get_posts( $args );
Here, I have passed order by,meta_value and order as the argument in get_posts() function and where “company” is the custom field. It could give me a sorted list of custom field “company” by arranging the meta value of that custom field in ascending order.
That’s it.Now you have records sorted by custom field. 🙂