Use datefield year as condition in Entity Query I'm performing an entity query that searches for nodes of my custom content type called 'news'.
The content type has a date field called 'field_date'. I want to be able to perform the query in a way that only produces the 'news' nodes that have a 'field_date' that fits within a specified year. Here is an attempt I made below.

$year = 2015;
$query = \Drupal::entityQuery('node')
  -&gt;sort('field_date', 'DESC')
  -&gt;condition('type', 'news')
  -&gt;condition('field_date', [date('Y','field_date'), $year], '=')
  -&gt;range(0, 10);


In the code above I'm attempting to only grab the year from the 'field_date' datefield and compare that to $year, 2015 in this case. However I receive SQL errors like:


  Invalid parameter number: number of bound variables does not match
  number of tokens


I'm guessing part of it is date('Y','field_date') doesn't know what 'field_date' is but I'm not sure how to go about it.

How do I make a year comparison on a datefield in a entityquery condition?
Assuming the data in the database looks like this: "2015-10-12T21:30:00", you can use a like query

-&gt;condition('field_date', $year . '%', 'like')


Your code revised with the change:

$year = 2015;
$query = \Drupal::entityQuery('node')
  -&gt;sort('field_date', 'DESC')
  -&gt;condition('type', 'news')
  -&gt;condition('field_date', $year . '%', 'like')
  -&gt;range(0, 10);