How to sort by condition in entityQuery I want to get a list that the items with a specific term in the top (like promote). The query would be like:

$query = \Drupal::entityQuery('node')
  -&gt;condition('status', 1)
  -&gt;condition('type', 'news')
  -&gt;sort('field_product_category', $tid, '&lt;&gt;') //Error &lt;&gt;
  -&gt;range(0, 5);


Can I put calculation in sort condition? I do not want to make 2 queries.
I don't think you can sort that way with an entity query.  But with a select query, you have more flexibility.

I would try something like this:

$database = \Drupal::service('database');

$query = $database-&gt;select('node_field_data', 'nfd')
  -&gt;fields('nfd', ['id']);


$query-&gt;condition('nfd.status', 1);
$query-&gt;condition('nfd.type', 'news');
$query-&gt;range(0, 5);

$query-&gt;leftJoin('field_product_category', 'pc', 'pc.entity_id = nfd.nid');
$query-&gt;addExpression('pc.field_product_category_target_id &lt;&gt; :target_tid', 'is_featured', [':target_tid', $target_tid]);

$query-&gt;orderBy('is_featured');
$query-&gt;orderBy('nfd.changed');

$matches = $query-&gt;execute()-&gt;fetchAll();