I've created a custom WordPress database table which I'm using to store 'likes'.

The columns I have are:

  • id
  • post_id
  • user_id

I'm now trying to get an array of post IDs that a particular user has 'liked'. So far I have this:

$my_array_of_post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM like WHERE user_id = %d", $user_id ) );

How can I ensure my $my_array_of_post_ids array is sorted so that the most recent 'like' is the first item in the array, the second most recent 'like' is 2nd, the third most recent 'like' is 3rd and so on?

1 Answer 1


Don't you mean this query:

"SELECT post_id FROM like WHERE user_id = %d ORDER BY id DESC"

where we assume that the id column is unique and increase with each like.

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.