Support

Account

Home Forums General Issues Order Posts by Most Popular Relational Post Object

Helping

Order Posts by Most Popular Relational Post Object

  • I’ve spent 3 days trying to figure this out and have gone nowhere. It’s a pretty simple question: How do I order a list of posts (CPT) based on how many times each post has been added as a Related Post Object to user profiles?

    I have setup 3 User Profile Relational (Post Object) fields where the User can select a single post in each field. I want to list the CPT items in the order of which has been most selected by Users via the Post Object fields.

    Hope that makes sense 🙂

  • This is not something that is directly possible and probably not easily done. You are trying to order posts based on information stored in 3 other fields that are not directly associated with the posts themselves. WordPress cannot be made to do this unless you want to write a lot of SQL queries and PHP code to get it done. In order to sort posts by something then that something needs to be information linked to the posts.

    To do this properly you would not only need to keep a total of the times a post is selected you’d also need to keep a list of all the profiles where it is selected.

    This is not a solution, just a rough idea of what you’ll need to do. You need to have 2 meta fields for each post. These fields do not need to be visible, they’ll just hold data. One field will be a total and the other field will keep a list of where it was selected in an array.

    Then you need to set up an acf/save_post filter that will run when the user profile is updated. In this filter you look at each field and compare it to the current list. If a post is added or removed then you add/remove it from the list as well as update the total +/-. With this in place you can then sort the posts by this total field.

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

The topic ‘Order Posts by Most Popular Relational Post Object’ is closed to new replies.