• So I have been doing some research on this for the past few hours and I am not sure if it is possible to do. I am trying to sort posts based on the amount of likes and tweets they have per post. I am retrieving the data for the likes and tweets and storing them in $likes and $tweets. Then I add the two for the total.($total) I would like to sort the posts based off of this number. (highest to lowest). Here’s where it gets tricky. I don’t retrieve the like and tweet data until AFTER the initial post query because I need to use the get_permalink() string to get the like and tweet data for each URL. So I am guessing I need to run a custom filter of some kind? not sure if this is possible…

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Sounds like you would have to manipulate the query results before the loop in order to get the data, then sort the posts accordingly. This would mean dealing with the WP_Query object internals, which sounds messy.

    Another approach, if the sort order doesn’t need to be accurate to the latest second would be to schedule a recurring event to update the numbers of all posts, stored as postmeta. Then your query could include an order by argument on this postmeta. You also manage how often you need to query tweets and likes by event frequency, instead of doing so for every page load. Probably much more efficient.

    There’s probably a few other approaches, but that would be my approach, FWIW.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom post order not based on anything in the database or in a custom field’ is closed to new replies.