Hello, Im trying to filter a pagelist programmatically so that it only shows one page for each unique author. That is to exclude pages with duplicate authors and only keep the most recent one. Thought I should check with you concrete veterans here on the forums to see if anyone of you have any suggestion of how to achieve this.
Thanks in advance!
Martin
Think you have found a bug? Instead of posting about it here, make an issue on Github here:
There are 3 ways I can think of:
1- if you want to only showing the latest post by 1 specific author on a page. Say itās the authorās page. Filter the list by user ID, order by date and grab the first one.
2- if you want to show a whole list of latest post by each author, order the list by user ID than run a foreach loop on it and compare dates. Unfortunately you canāt sort by more than 1 parameter
3- grab the SQL query generated by the PageList object and modify it to add a second sorting parameter and return only 1 result by author then copy the get method from the object and use it to return your page objects.
@mnakalay#2 is close. @svbrf is wanting to show a list of āstoriesā, newest first. Each individual author will have their latest post ONLY in the list. Iām thinking build an array of user idās as each new author is stumbled upon to thereafter check if a user has a page in the list already etc. Iām going to stab at this one and will post the code for further improvement!
I recently did a similar extension to the page list class as @mnakalay#3 to list pages containing forms built with Form Reform for a āFind my Formsā dashboard page. The tricky part was making sure that a page with N form component blocks on it was only listed once. I can imagine a āmost recent by authorā extension to a page list being a similar SQL structure.
Having said that, as @enlil suggests, variations of #2 would be easier to code, but could be tricky to paginate and could be expensive if there are 10 authors and the most recent by one of them is 1000 posts old. For a limited number of authors not requiring pagination, #2 would not fall into either of such problem areas.
OK, So Iām up well past my bedtime this morning. Got this nasty addiction to PHP Iām dealing with 2 for 1, 1 for 2. However you look at it, one is paginated, the other is not. Enjoy