I'm performing an entity query that searches for nodes of my custom content type called 'news'. I want to perform the query in a way that only produces the 'news' nodes that have a Authored Date that fits within a specific date . Here is an attempt I made below. enter image description here'

$year = 2018-02;
$query = \Drupal::entityQuery('node')
  ->condition('type', 'news')
  ->condition('created', [date('Y-M','created'), $year], '=')
  ->range(0, 10);

Try this function get_nodes_by_authored_month_date('news', '2018-02')

use Drupal\node\NodeInterface;

 * @param string $bundle_type
 * @param string $year_month
 * @return array|int
function get_nodes_by_authored_month_date($bundle_type = '' , $year_month = '') {

  $full_date = date_parse_from_format('Y-m', $year_month);
  //- Get number of days of month
  $number_of_days_of_month = cal_days_in_month(CAL_GREGORIAN, $full_date['month'], $full_date['year']);
  //- Get the timestamp of the first minute in the month 
  $first_minute_of_month = mktime(0, 0, 0, $full_date['month'], 1, $full_date['year']);
  //- Get the timestamp of the last minute in the month
  $last_minute_of_month = mktime(23, 59, 59, $full_date['month'], $number_of_days_of_month, $full_date['year']);

  $query = \Drupal::entityQuery('node')
    ->condition('status', NodeInterface::PUBLISHED)
    ->condition('type', $bundle_type)
    ->condition('created', [$first_minute_of_month, $last_minute_of_month], 'BETWEEN')
    ->range(0, 10);

  return $query->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.