MilenKo

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

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

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

  • Like 1

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"
  • Like 4

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");

 

  • Like 3

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");
 

 

  • Like 5

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 Sonia Margollé
      I have a problem and I don't understand. I have a profield table with different fields and a select field.
      I did like it's instructed and put "value=label".
      But when I'm trying to output the select field with var_dump (to debug),  it's indicated that the field is a string that contains the value.
      Why is it a string ?  when I try  myselectfield->value  or label or title I have an error saying I'm trying to get a property on a non-object ...
      Anyone ? 
    • By Vigilante
      This is a pretty typical thing. Open a page, it does a $pages query, finds 112 things and I list them on the page. There is pagination too, 20 per page.
      I want a sort dropdown box so the visitor can change alpha sort or whatever other sorts and filters I eventually use.
      My first thought is to just refresh the page with a url query like "page/?sort=az" and then adjust the query in the template code. But to do this, it will require a page refresh, which means the sort box goes back to its default, and not sure how it effects pagination.
      If I do a page refresh, I'll have to keep the sort settings in the session or something, to make sure I always apply correct sort on page load.
      But at the same time, I've already got the $pages array, it would be much nicer to shuffle it and update the page without a page refresh, rather than run the query over and over again.
      But then again, the pagination buttons already cause page refreshes, so that is already happening anyway, unless I convert the pagination buttons to also be no-refresh somehow.
       
      I figure this is a very common problem, to paginate results and give users a sorting/filtering option. What is the common methods for handling it? Session, url query, ajax? If I just add the sort to the url query, how do I make that work in the pagination links?
    • By mike62
      I'm trying to echo a field from another page, in my footer. I have a Website Settings page (id 1006) with several fields for general site settings, like store hours (field is named store_hours). In the footer, I have this:
      <?php $settingsPage = $pages->get(1006); echo $settingsPage->store_hours; ?> Shouldn't that output the contents of that field? Right now it's printing a "0" (zero) on the page.
      Elsewhere in the same footer template file, I have this code and it works fine:
      <?php $homepage = $pages->get(1); echo $homepage->body; ?> Does it have anything to do with the fact that the field is organized into one of these tabs, at the top of the page editor?
      (disclaimer: I'm new to PW, and have inherited this site from another developer; that's why I don't know how or why some of this stuff is set up the way it is).
      Thanks!

    • By ksymmons
      Hi,
      I'm creating a multilingual blog for a client. I'm using Multi-language fields, and it's working great. Essentially, there are two versions of each article: one in English and one in Spanish. 
      Now, I've created a Datetime field with a "Date/time picker" so that they can add a publication date (no time) to their blog entries. I want the month part of the date to be displayed in whatever language the article is being displayed in. Let's take the following example:
      1) "Un artículo de ejemplo", accessible via example.com/un-articulo-de-ejemplo/
      2) "A sample article", accessible via example.com/en/a-sample-article/
      In this example, for article number 1 I want the date to be "12 ene 2017", and for article number 2 I want it to be "12 jan 2017". In other words, the same format for both languages, but localised. And for some reason, I seem unable to do so and dates are always displayed in English.
      Here's the code I'm using to display the date field:
      <?= $post->publication_date; ?> How can I get this to work? Thanks.



    • By Robin S
      In response to a wishlist request...
      Field Save + Add New
      Adds a "Save + Add New" button when editing a field.

      Usage
      Install the Field Save + Add New module.
      When editing a field, click "Save + Add New" if you want to save the current field and start the process of adding a new field.
      Note: The button will not redirect you to "Add New" if you are performing some special action such as duplicating a field or adding a field to a template.
       
      https://github.com/Toutouwai/FieldSaveAdd/