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.
1 2 3 4 5 6 7 | $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. 🙂
Also Read:
To create extra field into category or Custom Taxonomy in WordPress
To Create custom field in Woocommerce Products Admin Panel
To Delete Taxonomies from database in WordPress
I hope you have enjoyed this article. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.