Hi guys
I’ve got a scenario where I’ll be adding some articles to a site with dates from the past (old articles on a new site y’see).
When I write new articles, I just hit Save and the site orders the articles on the publishedon field as usual. For the old articles I’m importing, I really want to use pub_date to set the old date.
The tricky bit is how to make a query that takes into account both fields. I can’t order by pub_date then publishedon unfortunately, as those fields with no pub_date will always come first as pub_date gets set to zero so the newer pages would always appear before the older ones.
Is there a way to compare the two fields in a mySQL query somehow and order by both fields so the dates are in ascending order?
The alternative I worked on was to pull all articles from the DB, loop through the results and put them into an array, setting the publishedon date to teh value of the pub_date field if it was greater than zero, then doing an array_multisort to order the array by date. This works fine, but has the enormous problem of having to pull ALL pages from the database to get the pages sorted by date and make sure you’re not missing any, then do another query to just grab the full content of the latest 10 documents based off this array.
So basically I’d really like a way to not have to pull all the page id’s and dates, create two arrays, do an array_multisort and then do another query. If it’s possible to sort on both fields in one query and be able to set a limit as well then that would save potentially tonnes of overhead (my alternative above is alright for a few hundred articles maybe, but much beyond that is just wasting server resources).
Any help would be appreciated, and I also appreciate it’s not the easiest request