Score:0

Query to order by specific item to be first and then to sort the rest with id desc

ng flag

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');
Ivaylo Tsandev avatar
us flag
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.
Jaypan avatar
de flag
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'.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.