I am returning news to my homepage in a carousel. I am using the below query to return the latest 9 articles based on the "created" date. The client however wants to back date some articles (so admin > content > edit node > authored by and change the created date). This however does not seem to work as keeps returning the articles in the actual date order they were created, even if the date was changed manually:
$query = \Drupal::entityQuery('node');
$query->accessCheck(FALSE)
->condition('type', 'news')
->condition('langcode', \Drupal::languageManager()->getCurrentLanguage()->getId(),"IN")
->sort('created', 'DESC')
->range(0, 9);
$nids = $query->execute();
$entities = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);
What am I doing wrong? It seems to work fine within the actual news page view. The view itself has the sort set as "Content: Authored on (desc)" and this returns the data as expected, but the preprocessor query does not.
EDIT:
The current recommendation is to change the sort to "changed", but this unfortunately does not work. I have a feeling the issue is related to translations. The following is the 1st result in the array. I deliberately went ahead and changed the "authored" date to 2002 to ensure it goes last, yet, it's still first. If you look at the values however, I think this is where the issue happens:
[created] => Array
(
[da] => 1647950253
[x-default] => 1016357523
[es] => 1647950332
[fi] => 1647950277
[fr] => 1647949861
[nb] => 1647950235
[nl] => 1647950348
[sv] => 1647950211
[en-au] => 1647949797
[en-gb] => 1647947671
[en-us] => 1647947639
[bel-fr] => 1647950381
[bel-nl] => 1647950361
)
Based on this result, it may be that the sort looks for the 1st "created" date, being DA, but in our case the language is the x-default
Can I change the sort by to ensure it sorts by the actual language?
->sort('changed', 'DESC')