Jump to content

Show pages published by a time interval (day, week, month etc.) without a date field


MilenKo
 Share

Recommended Posts

Hello all.

I am trying to find a way to have a query with all pages that were published on a specific date (today for example). I read a few posts where people had a specific date field and were limitting the results by that, however is there a way to filter results without a specific date field? As far as I am able to publish the timestamp using $page->created logically I should be able to filter by the result of it? What is the best way to accomplish a filter for a day, week, month etc.?

Link to comment
Share on other sites

27 minutes ago, MilenKo said:

I read a few posts where people had a specific date field and were limitting the results by that, however is there a way to filter results without a specific date field?

It's the same, just replace $page->custom_date_field with $page->created or $page->published

  • Like 1
Link to comment
Share on other sites

@Robin S Thanks for the advise. I thought so, but I am getting an error on my code:

<ul class="cs-recipes"> 

<?php
$start = strtotime( date('Y-m-d') . " 00:00:00");
$end = strtotime( date('Y-m-d') . " 23:59:59");

if($pages->get('/recipes/')->children) {
$items = $pages->find("template=recipes-inner, $page->published>$start, $page->published<$end, sort=date, limit=9"); 
foreach($items as $rotd) { ?>

	<li class="cs-recipe">
		<div class="cs-recipe-image">
			<div class="cs-recipe-details-button">
				<a href="recipe_single_layout_1.html">Details</a>
			</div>
			<img src="<?=$rotd->recipe_image->url?>" alt="<?=$rotd->recipe_image->description?>">
		</div>
		<div class="cs-recipe-meta">
			<span><i class="fa fa-hourglass-half"></i> <?=$rotd->recipe_cooktime?></span>
			<span><i class="fa fa-cutlery"></i> <?=$rotd->recipe_servings?></span>
		</div>
		<h3>	
			<a href="<?=$rotd->httpUrl?>"><?=$rotd->title?></a>
		</h3>
	</li>  
<? } ?>
	
</ul>

Any ideas where did I messed it up this time? :)

Link to comment
Share on other sites

From the documentation for selectors

<?php
// single condition
// pages published today, sorted new->old
$recentItems = $pages->find("template=product, my_date_field>=today, sort=-my_date_field");

// multiple conditions with AND operator
// pages published this year until today
$thisYear = date('Y-01-01'); // 2017-01-01 or 2011-01-01 etc
$recentItems = $pages->find("template=product, published>=$thisYear, published<=today");

 

Link to comment
Share on other sites

You shouldn't put $page-> in there, just use the name of the field. And your sort field must be an existing field.

$items = $pages->find("template=recipes-inner, published>=$start, published<=$end, sort=-published, limit=9"); 

Also you don't have to manually build the boundaries of today, you can just use today

$items = $pages->find("template=recipes-inner, published>=today, sort=-published, limit=9"); 

In fact, you can use any valid PHP date that you can use with DateTime constructor. http://php.net/manual/en/class.datetime.php

published>=today
published>=1365436783
published>=2013-04-08
published>=4/8/2013
published>=8.4.2013
published>="April 8, 2013"
  • Like 4
Link to comment
Share on other sites

OK, that seemed to work and shows some results.

Now if I want to show dates 2 or 3 days ago, would it be correct to have $start = strtotime( date(('Y-m-d')-2) . " 00:00:00"); or there is a better/correct way of doing it? 

Also let's say I need to show the pages for last 7 days?

Link to comment
Share on other sites

@abdus I tested your code using DateTime class, but am getting a server error 500:

        $today = new DateTime();
        $aFewDaysAgo = $today->modify('-2 days');
        if($pages->get('/recipes/')->children) { 
        $latest = $pages->find("template=recipes-inner, published>=$aFewDaysAgo, sort=-published, limit=9");

and the error in exceptions.txt is: Object of class DateTime could not be converted to string.

Link to comment
Share on other sites

4 hours ago, MilenKo said:

$today = new DateTime();

Without a backslash class names resolve to current namespace (ProcessWire), and since DateTime class is in global namespace, you should put \ before DateTime.

$today = new \DateTime();

Also, I've realized that I am getting the same error, but $pages->find() is still returning a result (because when DateTime object is converted to string it turns into an empty string '', and the selector becomes "published>'' ", which returns all pages). You can fix the error using:

$today = new \DateTime();

// use UNIX timestamps
$aFewDaysAgo = $today->modify('-2 days')->getTimestamp();
$lastYear = $today->modify('-1 year')->getTimestamp();

// or convert to ISO format
// $aFewDaysAgo = $today->modify('-2 days')->format('c');
// $lastYear = $today->modify('-1 year')->format('c');

$latestPages = $pages->find("published>=$aFewDaysAgo");
$pagesSinceLastYear = $pages->find("published>=$lastYear");
 

 

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...