Forum

Thread tagged as: Blog

Blog Filter on Date

Hi,

I am using the Perch Blog app and I want to use a different id to display the months and entry dates of the items.

By default, the blog app uses 'postDateTime', but I want to use a different date id, such as 'mypostDateTime', as I want to have a different date to the entry than the publishing date.

This all works fine for returning entries etc, but I when I try to get the date filters to work, it fails. To give you an example, my 'months_month_link.html' template is now:

<perch:before>
<ul>
</perch:before>
    <li><a href="archive.php?year=<perch:blog id="mypostDateTime" format="Y" />&amp;month=<perch:blog id="mypostDateTime" format="m" />"><perch:blog id="mypostDateTime" format="%B" /></a></li>
<perch:after>
</ul>
</perch:after>

and in my post,html template, I have added:

<time class="dt-published" datetime="<perch:blog id="mypostDateTime" type="date" label="Event Date" time="true" format="Y-m-d H:i:s" divider-before="Publishing" />">
<a href="archive.php?year=<perch:blog id="mypostDateTime" format="Y" />&amp;month=<perch:blog id="mypostDateTime" format="m" />"><perch:blog id="mypostDateTime" type="date" time="true" format="%d %B %Y" /></a>
</time>

The issue is that the filter is not returning any months when called from:

<?php perch_blog_date_archive_months(); ?>

Ahh, I've just suddenly thought whilst writing this, I bet I need to do a custom perch call as the above is intrinsically attached to the id postDateTime ?

Anyway, I will take a look at this thought, but any suggestions welcome !!!

Thanks,

Andy

Andrew Kennedy

Andrew Kennedy 0 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd use something like

perch_blog([
    'filter' => 'mypostDateTime',
    'match' => 'lte', 
    'value' => date('Y-m-d H:i:00'),
]);

Hi Drew,

I didn't see your reply ! Thanks for getting back to me. This has put me on the right path, but I am still struggling to get it to work as I want.

Previously, I was trying to make things much more complicated that it needed to be. Ultimately, what I want to do is replicate exactly what the call: <?php perch_blog_date_archive_months(); ?> does, but rather than being based on the id postDateTime, it is based on a different date, mypostDateTime, which is actually a future date

In my archive.php page, I have replaced my <?php perch_blog_date_archive_months(); ?> call for:

<?php perch_blog([
    'filter' => 'mypostDateTime',                                   
    'match' => 'gt', 
    'value' => date('Y-m-d H:i:00'),
    'template' => 'months_year_link.html'
]);
?>

The idea being, I can then push through all these returned results using the same templates (this and months_month_link.html) to get the same type of listing. As such, in months_month_link.html, I have changed all the id=postDateTime to id=mypostDateTime.

months_year_link.html

<perch:before>
<h5><strong>Date:</strong></h5>
<ul>
</perch:before>
    <li><a href="archive.php?year=<perch:blog id="year" />"><perch:blog id="year" /></a>
    <perch:blog id="months" encode="false" />
    </li>
<perch:after></ul></perch:after>

months_month_link.html

<perch:before>
<ul>
</perch:before>
    <li><a href="archive.php?year=<perch:blog id="mypostDateTime" format="Y" />&amp;month=<perch:blog id="mypostDateTime" format="m" />"><perch:blog id="mypostDateTime" format="%B" /></a> 
    </li>
<perch:after>
</ul>
</perch:after>

Now, with the above, it doesn't get past returning the actual year, that is, the template months_year_link.html returns no results.

Swapping, the template in the above code to months_month_link.html gives me a listing of all the posts by month, but individually, i.e. listed November, December, December, January etc, rather than entries being grouped into single months.

Hope your still with me :-)

Not sure why this is not passing the date(s) through. Perhaps I just cannot use the original perch templates in this way and need to create my own bespoke ones, but as I have replaced all id names (postDateTime for mypostDateTime) I had thought it might just pass through ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Make sure you include the type attribute (here it's type="date") otherwise Perch won't know which type of field it's outputting.

Sadly doesn't seem to make a difference.

It cannot get past the first stage of passing the year to the months_year_link.html template in the querystring.

The code in my previous entry does not render anything in the querystring. That is:

months_year_link.html

<perch:before>
<h5><strong>Date:</strong></h5>
<ul>
</perch:before>
    <li><a href="archive.php?year=<perch:blog id="year" type="date" />"><perch:blog id="year"  type="date" /></a>
    <perch:blog id="months" encode="false" />
    </li>
<perch:after></ul></perch:after>

Returns nothing. Where does the blog id="year" come from. Is it system set.

I'm presuming <?php perch_blog_date_archive_months(); ?> calls months_year_link.html template and passes the querystring ? from postDateTime.

<perch:blog id="months" encode="false" /> - this call must call the months_month_link,html template ?

So, when I call my posts from the archive using:

archive.php

<?php perch_blog([                                  
'filter' => 'mypostDateTime',                                   
'match' => 'gt', 
'value' => date('Y-m-d H:i:00'),
'template' => 'months_year_link.html'
]);
?>          

Nothing is being passed in the querystring to the template.

As such, I think the whole idea of trying to re-use the months_year_link.html template and the months_month_link.html template with mypostDateTime rather than postDateTime is just not going to work.

Just very confused.

I can see that I could keep the call in the archive.php page the same, but then change the template months_year_link.html template to something like:

months_year_link.html

<perch:before>
<h5><strong>Date:</strong></h5>
<ul>
</perch:before>
    <li><a href="archive.php?year=<perch:blog id="mypostDateTime" type="date" />"><perch:blog id="mypostDateTime" type="date" format="%Y" /></a>
    </li>
<perch:after></ul></perch:after>

and then somehow modify this template further to say, if blog entry exists for mypostDateTime display first line of list as year (only once), then call a month sub template and look for different blog entries that belong to that year with different months and only display the month once.

This is what the whole months_year_link.html template and the months_month_link.html template are doing, but I just cannot see how I can pass the correct querystring.

Just very confused and tired.

Hope this all makes sense.

I suppose in a nutshell, what I need is some different version of <?php perch_blog_date_archive_months(); ?> that works for id="mypostDateTime" rather than id="postDateTime"

Thanks,

Andy

Drew McLellan

Drew McLellan 2638 points
Perch Support

You're not going to get anywhere using those archive functions with a custom date field. They're hardwired to use the default date field.

Exaclty, hence why I was using the call in the archive.php file

<?php perch_blog([
    'filter' => 'mypostDateTime',                                   
    'match' => 'gt', 
    'value' => date('Y-m-d H:i:00'),
    'template' => 'months_year_link.html'
]);
?>

but what I am saying is that, I also do not think that I can use the months_year_link.html and the months_month_link.html template almost as they are, just with a different date id.

I think I am going to have to create a bespoke template to replicate what both of these do.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's right.

Thanks Drew, at least I can set off on the correct path now.

I got dragged off onto a new job, but I am so close to resolving this issue.

In my archive.php file, I am calling:

<?php perch_blog_custom ([                                  
    'filter' => 'mypostDateTime',                                   
    'match' => 'gt', 
    'value' => date('Y-m-d H:i:00'),
    'template' => 'events_date_link.html'
    ]);                                 
?>

which seems to be working fine. The issue now, is creating the custom template to produce a result such as:

  • 2018
    • November
    • December
  • 2019
    • December

where I have a listed set of links given the returned results by mypostDateTime rather than postDateTime.

Currently, my template looks like:

<perch:before>
<ul>
</perch:before>
<perch:if different="mypostDateTime" format="%Y">
    <li>
        <a href="archive.php?year=<perch:blog id="mypostDateTime" format="%Y" />"><perch:blog id="mypostDateTime" format="%Y" /></a>    
                <perch:if different="mypostDateTime" format="%B">
                <ul>
                    <li>
                        <a href="archive.php?year=<perch:blog id="mypostDateTime" format="%Y" />&amp;month=<perch:blog id="mypostDateTime" format="m" />"><perch:blog id="mypostDateTime" format="%B" /></a>    
                    </li>
                </ul>
                </perch:if>             
            </li>
        </perch:if>
<perch:after>
</ul>
</perch:after>

which so nearly works. It returns the years correctly, but only a single month under each year heading, not multiple months.

For example, rather than being:

  • 2018
    • November
    • December
  • 2019
    • December

It is returning:

  • 2018
    • December
  • 2019
    • December

From this, I can see that the sub perch:if different is not 'looping', even though, I would anticipate that when I compare months in the sub perch if, they will be different.

Any ideas ?

Thanks,

Andy

Drew McLellan

Drew McLellan 2638 points
Perch Support

Make sure you set sort and sort-order so that the results come out in the expected order.

Thanks Drew, that's great.

Still have the original issue though of the template not returning all the results. In other words, for a given year, it is only returning a sinlge months entries, rather than all the different months.

Currently returning:

  • 2018
    • December
  • 2019
    • December

but I have entries in the DB, so that the structure delivered from the template should be:

  • 2018
    • November
    • December
    • 2019
    • April
    • December

As such, I am presuming that my events_date_link.html template that I call:

<perch:before>
<ul>
</perch:before>
<perch:if different="mypostDateTime" format="%Y">
    <li>
        <a href="archive.php?year=<perch:blog id="mypostDateTime" format="%Y" />"><perch:blog id="mypostDateTime" format="%Y" /></a>    
                <perch:if different="mypostDateTime" format="%B">
                <ul>
                    <li>
                        <a href="archive.php?year=<perch:blog id="mypostDateTime" format="%Y" />&amp;month=<perch:blog id="mypostDateTime" format="m" />"><perch:blog id="mypostDateTime" format="%B" /></a>    
                    </li>
                </ul>
                </perch:if>             
            </li>
        </perch:if>
<perch:after>
</ul>
</perch:after>

is failing to loop during the <perch:if different="mypostDateTime" format="%B"> section. If I add new entries in different years, the listing builds correctly, i.e. adding a new entry in for 4th October 2020 gives.

  • 2018
    • December
    • 2019
    • December
    • 2020
    • October

But if I added another entry for April 2020, it only shows the October link, as per all the other years.

Hope this explains it better.

Maybe I need some for of * if not last entry* to create a month loop within the year ????

Drew McLellan

Drew McLellan 2638 points
Perch Support

If you don't set a count you'll only get the first page of results. Would that explain what you're seeing?

Sadly not.

My archive.php and template events_date_link.html (as above) are trying to replicate the standard blog call of <?php perch_blog_date_archive_months(); ?> but with a different date.

The listing of the years and months is thus a direct result of the output from my events_date_link.html template. The custom call within archive.php is returning ALL results, as when I click on the year, I will see all entries from all the months of that year and consequently, selecting another year from the list will do the same thing, but for that specific year.

The sub list within the year, just returns entries from a single month, and does not cycle through. So for example, 2018 might have four entries. 2 in December, 1 in October and 1 in April. Currently, it just returns December as a month, which works correctly, as in the link when selected returns the two entries for December 2018, but it does not display the navigation links for October or April.

After testing, I can see that the month displayed in the list is in reference to the last entry made into the database.

So, if my last entry was for mypostDateTime of 4th December 2018, the list would be:

  • 2018
    • December

but if I change that same last entry to have a mypostDateTime of 4th November 2018, the list will be:

*2018 + February

So, for some reason, the template is not getting past, or just returning the last entry in the database's month wrt that year.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show me the perch_blog_custom() call you're using in each case?

Hi Drew,

To replace the original <?php perch_blog_date_archive_months(); ?> in archive.php, I am using the call:

<?php perch_blog_custom ([                                  
    'filter' => 'mypostDateTime',                                   
    'match' => 'gt', 
    'value' => date('Y-m-d H:i:00'),
    'template' => 'events_date_link.html',
    'sort-order' => 'ASC',
    ]);                                 
?>

As you can see, this then call's my events_date_link.html template, which is trying to duplicate the standard months_year_link.html which is called from <?php perch_blog_date_archive_months(); ?>.

My code renders the listing:

*2018 + December *2019 + October *2020 + December

Where the month listed is refers to the last entry made in the database, but does not reflect that multiple entries have been made in different months for each year, i.e. 2018 has entries for October, November and December.

Is this what you were after ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to set sort for sort-order to work. Also you'll only get 10 posts, paginated, unless you set count and also turn off pagination. Blog does those by default.

You might be best to test with a basic template (no conditionals) to make sure you're getting the correct set of data first before you try to manipulate it into a list.

Ok, so I have been testing this for quite sometime trying to get my head around exactly what is happening here.

Firstly, I have added in sort and sort-order to my call, so we now have:

<?php perch_blog_custom ([                                  
    'filter' => 'mypostDateTime',                                   
    'match' => 'gt', 
    'value' => date('Y-m-d H:i:00'),
    'template' => 'events_date_link.html',
    'sort' =>  'postDateTime',
    'sort-order' => 'ASC',
    'count' => 100
    ]);                                 
?>                          

The first issue I have is that I do not want to sort by postDateTime but mypostDateTime, but this will not work for me, if it is used as a filter and to be sorted by.

With a stripped down template, the results returned are exactly what I expect, just sorted by postDateTime, rather than mypostDateTime

<perch:before>
<ul>
</perch:before>
    <li>
        <a href="archive.php?year=<perch:blog id="mypostDateTime" format="%d %B %Y" />"><perch:blog id="mypostDateTime" format="%d %B %Y" /></a>    
    </li>
<perch:after>
</ul>
</perch:after>

The above gives me a date list of all the entries (displayed as mypostDateTime), but sorted on postDateTime. Fine, perfect.

Now, trying to get things into the format like the original blog templates, that is something like:

  • 2018
    • January
    • February ........
  • 2019
    • January
    • February ........

etc etc, means that I need to adjust my template and add in some conditionals. Starting at the top level, I need to return all the entries, grouped by year.

<perch:before>
<ul>
</perch:before>
   <perch:if different="mypostDateTime" format="%Y">
    <li>
        <a href="archive.php?year=<perch:blog id="mypostDateTime" format="%d %B %Y" />"><perch:blog id="mypostDateTime" format="%d %B %Y" /></a>    
    </li>
  </perch:if>
<perch:after>
</ul>
</perch:after>

This returns a listing of all the years, but because it is sorted on postDateTime, the listing is infact all the years where items have been published postDateTime grouped, which is confusing, but understandable as we are sorting on postDateTime.

As such, I then looked at completing my issue for postDateTime so as not to confuse matters further.

<perch:before>
<ul>
</perch:before>
   <perch:if different="postDateTime" format="%Y">
    <li>
        <a href="archive.php?year=<perch:blog id="postDateTime" format="%Y" />"><perch:blog id="postDateTime" format="%Y" /></a>    
    </li>
  </perch:if>
<perch:after>
</ul>
</perch:after>

returns a listing of the entries grouped by year, sorted by postDateTime, so yes, excellent all working so far.

So, adding in my next conditional which is, once I have grouped by year, I want to group by month, I use:

<perch:before>
<ul>
</perch:before>
   <perch:if different="postDateTime" format="%Y">
    <li>
        <a href="archive.php?year=<perch:blog id="postDateTime" format="%Y" />"><perch:blog id="postDateTime" format="%Y" /></a>
       <perch:if different="postDateTime" format="%B">
       <ul>
        <li>
           <a href="archive.php?year=<perch:blog id="postDateTime" format="%Y" />&amp;month=<perch:blog id="postDateTime" format="%B" />"><perch:findevent id="postDateTime" format="%B" /></a> 
        </li>
      </ul>
</perch:if> 
    </li>
  </perch:if>
<perch:after>
</ul>
</perch:after>

This returns the listing of years, but only with a single month attributed to each year, not a listing of months I would expect for the entries based on their postDateTime.

The month listed relates to the entry with the postDateTime furthest in the past, so I would presume, the conditional is cycling through all the year entries and, rather than displaying each month when there is a difference, it is just displaying the last month in the list, as this is the last one to have a difference.

So perhaps, <perch:if different="postDateTime" format="%B"> is the wrong call to make, as I had presumed it would cycle through and see that for whatever it's parent year, it would create a link if the month of the postDateTime is different, which is what I have tried to do in my template.

Hmmm.

Odd though that the call in my template earlier, which cycles through the years in the same way, works.

Any thoughts ? Everything is working, it's just this list manipulation element.

Drew McLellan

Drew McLellan 2638 points
Perch Support

The first issue I have is that I do not want to sort by postDateTime but mypostDateTime, but this will not work for me, if it is used as a filter and to be sorted by.

Ok. You need to address that before anything else. Add this to a page and enable debug. What does it output?

perch_blog_custom([  
'sort' => 'mypostDateTime',  
'sort-order' => 'ASC',
]);

You have to set sort for sort-request to work. Likewise you'll just get 10 posts, paginated, except if you set check and furthermore kill pagination. Blog does those as a matter of course.

You may be best to test with an essential format (no conditionals) to ensure you're getting the right arrangement of information first before you attempt to control it into a rundown.

Regards,

https://redboxtvdownloadapp.com

https://redboxtvdownloadapp.com/redbox-tv-firestick-amazon-tv/