Wordpress WP_Query custom post order
Recently, I had to build a Wordpress Admin widget area which enables a user to select, change order and save a list of posts. Selected post IDs are stored in an
array() which can be found in the options table. It looks something like this:
So when it comes to rending the posts in the array, I just create a new
WP_Query(), like so:
Wordpress automatically queries posts in a reverse chronological order (newest post first), or if set - by
orderby value. However, what if I need to keep the queried posts in specific order listed in my
$post_list array? This sort of condtion cannot be passed into
WP_Query() as an argument, but it can be done after query is completed. After some messing around by creating loops and filtering the object, the best solution was to use the usort() PHP function.
And that’s it.