5

I'm trying to get elements in a section created on a specific date. How do I do that?

This is what I've been trying with:

$criteria               = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section  = 'sectionHandle';
$criteria->postDate     = '2014-11-04';
1
  • Can anyone suggest an update to the accepted answer that works in Craft 4? I'm getting errors to the effect of "Class "modules\modulename\services\ElementType" not found'. Commented Oct 26, 2023 at 0:06

1 Answer 1

9

Something like this should work:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section  = 'sectionHandle';
$criteria->postDate = array('and', '> 2014-11-03 23:59:59', '< 2014-11-05 00:00:00');
$results = $criteria->find();
4
  • This works great Brad. Is there any way I can make it just by the date and not include the time? I tried yii-syntax with postDate = ['IN', '2014-11-04'] but that got me a "Fatal error: Call to a member function getTimestamp() on a non-object in /craft/app/helpers/DbHelper.php on line 597". Any ideas? Commented Mar 19, 2015 at 17:51
  • Nothing I can think of...
    – Brad Bell
    Commented Mar 19, 2015 at 17:56
  • Is there another way you would recommend me fetching elements from a section posted at a specific date using Craft build in API functions? Commented Mar 19, 2015 at 18:00
  • Not without directly querying the database.
    – Brad Bell
    Commented Mar 19, 2015 at 22:26

Your Answer

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

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