I am into a unique requirement where I need to sort records to show specific data first and then rest sort by id descending. I am able to see records using SELECT * FROM jobs where type='jobs' and status=1 order by field_job_id='J5-00104' desc,field_job_id desc;.

Is there any way to achieve this in Drupal way? BTW I am trying to manipulate a query with the following code.

$query = \Drupal::entityQuery('node');
$query->condition('type', 'jobs');
$query->condition('status', 1);
$query->sort('field_job_id="J5-00104"', 'DESC');
$query->sort('field_job_id', 'DESC');
  • There are several ways I can think of, but maybe the fastest one would be to make 2 queries - one with the specific item that should float on top and then create a second request, ignoring this specific item. Then combine the two results and you'd be done. For sure there are better 'Drupal' ways, but they would require a bit more time and work. One of them would be to add a flag to this entity type and sort by it. Something like 'promoted' for example.
    – itsandev
    Commented Jun 14, 2023 at 12:37
  • I would add a field to the items flagging the item that should be listed first. Then set the order to be first on 'flagged'.
    – Jaypan
    Commented Jun 14, 2023 at 19:01


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.