MilenKo

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

11 posts in this topic

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.?

Share this post


Link to post
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

1 person likes this

Share this post


Link to post
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? :)

Share this post


Link to post
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");

 

Share this post


Link to post
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"
4 people like this

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

You can use DateTime class. http://php.net/manual/en/class.datetime.php

$today = new \DateTime();
$aFewDaysAgo = $today->modify('-2 days');
$lastYear = $today->modify('-1 year');

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

 

3 people like this

Share this post


Link to post
Share on other sites

Ok, so it looks like DateTime class would do the trick if I ever want to present some pages in a specific timeframe.

Thanks again @abdus & @Robin S

Share this post


Link to post
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.

Share this post


Link to post
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");
 

 

5 people like this

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By pppws
      hey there,
      i'm using processwire for the first time, so maybe it's a dumb question. but i'm trying to have an options field which values are the users of the processwire. i don't need the values to be displayed, it's just for the administration of the page. but each time a new user is created / a user is deleted the options field should be updated automatically. is something like that possible?
    • By Sipho
       

      I have an issue where I want different formats for creating one page. When you are creating a new page I want it to offer different types of fields from when you are editing it.
      For example, I have a field called countries which is a multiple page reference field. This is desirable as it is in a very easy to edit format. Currently, I am using selectize.js which makes it possible to search for the pages and add them in a tag fashion. This is how I want it to be when a page is already created. However, when creating a new page it is often easier to just paste a list of all the countries as text. This is because the data is coming from an old website where the countries are written like this:
      I can code something which converts this text into the multiple page reference fields but I am not sure how to go about it. At first I tried making another template which had fields that were in this “quick” format. Then I planned to make it such that when you add one of these quick pages it creates a page in the standard format and deletes itself. This has it’s own problems such as where to place the new page, what title and name to give it, whether to have just one quick page or multiple and when to delete it. It just didn’t seem right. 
      Another possibility would be to show special quick fields when creating a new standard page but hide them when the page is saved and show the standard fields. I am not to sure how to achieve this though. 
      Does anyone know a better way of going about this? I feel like this is a simple problem which already has an elegant solution.
       
    • By franciccio-ITALIANO
      Hi, 

      In one Page I would like to put a lot of url.
      There is url's field.
      Now, I add url's field to one template, but I don't know to add 2 or 3 or 10 url's field in that same template... the problem is simple, but processwire don't allow it...

    • By Robin S
      Note: this functionality is now built into AdminOnSteroids.
       
      I'd hate to know how much accumulated time I have spent and how much eye-strain I have experienced over the last couple of years, hunting through the 675 icons in the "all icons" view for the one I want. Today I finally got around to doing something about it.
      IconsFilter
      Allows the "all icons" view in InputfieldIcon to be filtered by name.
      Usage
      Install the IconsFilter module.
      When viewing "all icons" in InputfieldIcon (Advanced tab of field/template settings) you can filter the icons by name using the filter input at top right.
       

       
      https://github.com/Toutouwai/IconsFilter
       
      @tpr, something that could be merged into AdminOnSteroids?
    • By MilenKo
      Hello my friends. Today I started working on my recipe website again and it was the turn to show recipes (pages) that has a specific category assigned in a field.
      In my NowKnow project for categories I used a parent page where inside of it I had the children and everything seemed to be super easy. This time, however, I decided to change the approach so I created a parent page Recipe categories and assigned to it my 'category' template. Inside the parent I added a few categories that I want to be able to select via PageReferrence field 'recipe_category'.
      The parent template would show all the categories represented by a title and an image - that part is done and works fine. 
      Now what I am trying to achieve is to have a few recipes having the 'recipe_category' field equal to Bakery for example, and then when I point to the category URL to get only the recipes that have Breakfast selected in. From what I know the perfect approach to achieve that would be to use $input->urlSegment as to select the name of the category from the URL and then filter the pages adding to selectors: recipe_category=$category.  Following Ryans earlier instructions about the urlSegment and an example found here in the forum, I got this code to fit my fields names:
      <?php if($input->urlSegment1 == 'category' && $input->urlSegment2) { $name = $sanitizer->pageName($input->urlSegment2); $category = $pages->get("template=categories-list, title=$name"); if($category->id) { $q = $pages->find("template=recipes-inner, recipe_category=$category"); } } ?> After adding the code, I enabled the URL segments for both: 'category' and 'category-list' templates. Browsing the URL for the Bakery category:
      http://food.pw/category/bakery/ (the domain name is not a typo, but PW is on my local server) I was supposed to get the $category to get the value of 'bakery'. However instead of that I am getting nothing.
      What am I missing in the big picture as I am sure it is again something silly but I spent almost the whole day trying to figure it out and still got no progress?
      P.S. trying to change the urlSegment number to 3 did not help either