Title: WP Query orderby custom function
Last modified: May 2, 2021

---

# WP Query orderby custom function

 *  Resolved [robertherold](https://wordpress.org/support/users/robertherold/)
 * (@robertherold)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/wp-query-orderby-custom-function/)
 * Hello,
 * I would like to sort the posts by a custom function.
    How could I do that?
 * > [View post on imgur.com](https://imgur.com/a/ECG3q1y)
 * Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/wp-query-orderby-custom-function/#post-14389728)
 * Assuming a post’s average rating is saved in the postmeta table somewhere, include
   the associated keyname as a “meta_key” argument, but do not supply a “meta_value”
   arg (unless you want to limit posts to those with that one value). Then for the“
   orderby” arg, specify “meta_value_num”, assuming the saved ratings are numeric.
 * If the ordering criteria does not exist in the DB and must be calculated on the
   fly, you could apply a custom sorting algorithm to the query’s `posts` property
   by using PHP’s `usort()` function.
    [https://www.php.net/manual/en/function.usort.php](https://www.php.net/manual/en/function.usort.php)
 *  Thread Starter [robertherold](https://wordpress.org/support/users/robertherold/)
 * (@robertherold)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/wp-query-orderby-custom-function/#post-14390072)
 * Thank you very much! You helped a lot! 🙂
    I used to use the usort() function,
   but for some reason I didn’t think of it now.
 * Thanks again! 😉

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WP Query orderby custom function’ is closed to new replies.

## Tags

 * [custom function](https://wordpress.org/support/topic-tag/custom-function/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [robertherold](https://wordpress.org/support/users/robertherold/)
 * Last activity: [5 years, 1 month ago](https://wordpress.org/support/topic/wp-query-orderby-custom-function/#post-14390072)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
