We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question

Expiring Content

I have created some custom content on a site which acts as a listing of events. I wondered if there was a means of expiring content in Perch, such that after the date of the event they were no longer displayed?

Phil Bowell

Phil Bowell 0 points

  • 2 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

Add a date to your template, and then filter on that using perch_content_custom.

https://docs.grabaperch.com/functions/content/perch-content-custom/

Thanks Rachel, I'm much more of a designer than developer so programming something like this is well out of my knowledge. I understand to filter on the start date of my content, and to use match as a means of saying only show it if… but it's saying today date is what to check against I don't understand.

This is my thinking:

<?php $today = getdate(); ?>

<?php perch_content_custom('Events', array(
          'page' => '/events/index.php',
      'template' => 'event_listing.html',
          'sort' => 'eventStart',
          'filter' => 'eventStart',
          'match' => 'gte',
          'value' => $today,
)); ?>

That produces this in debug and so I'm not really sure where to go.

SELECT * FROM perch2_pages WHERE pagePath='/events/listing.php' LIMIT 1
[8] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1 ORDER BY pageTreePosition ASC
[1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/events/listing.php' LIMIT 1
[2] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-009-001', '000-009', '000') ORDER BY pageTreePosition DESC
[8] Using template: /templates/navigation/item.html
[5] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/events/listing.php' OR regionPage='*' ORDER BY regionPage DESC
[1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Events' AND (regionPage='/events/index.php' OR regionPage='*')
SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='eventStart' WHERE ((idx.regionID=135 AND idx.itemRev=34)) AND ((idx.indexKey='eventStart' AND idx.indexValue >= )) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval ORDER BY sortval ASC
Invalid query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ' at line 3
Using template: /templates/content/event_listing.html
Array
(
    [type] => 2
    [message] => PDO::quote() expects parameter 1 to be string, array given
    [file] => /var/www/cmj.vhx.host/perch/core/lib/PerchDB_MySQL.class.php
    [line] => 382
)
Duncan Revell

Duncan Revell 78 points
Registered Developer

getdate() returns an array, which is causing the error.

Try date() instead.

Thanks Duncan, that is no longer causing an error however, it is still displaying an item with an older start date than today.

Using this it still returns this items with older dates on.

<?php $today = date('j M Y'); ?>

<?php
    perch_content_custom('Events', array(
          'page' => '/events/index.php',
      'template' => 'event_listing.html',
          'sort' => 'eventStart',
          'filter' => 'eventStart',
          'match' => 'gte',
          'value' => $today,
    ));
 ?>

I've done a few things with perch calendar, but not had to attempt this, and i'm not sure perch calendar can do it. so I think I'd tackle this using javascript and css...

Can't take credit for the code, but I think its what you could be looking for.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript">

    window.setInterval(function(){

        var countOfLinksThatAreValid = 0;

        var current = new Date();
        var linkOneExpirationDate = new Date("January 27, 2015 19:49:59")
        var linkTwoExpirationDate = new Date("January 27, 2015 19:05:30")
        var linkThreeExpirationDate = new Date("January 27, 2015 19:06:30")

        if (current.getTime() <= linkOneExpirationDate.getTime()) {
            $('#linkOne').show();
            countOfLinksThatAreValid = countOfLinksThatAreValid + 1;
        }
        else {
            $('#linkOne').hide();
        }

        if (current.getTime() <= linkTwoExpirationDate.getTime()) {
            $('#linkTwo').show();
            countOfLinksThatAreValid = countOfLinksThatAreValid + 1;
        }
        else {
            $('#linkTwo').hide();
        }

        if (current.getTime() <= linkThreeExpirationDate.getTime()) {
            $('#linkThree').show();
            countOfLinksThatAreValid = countOfLinksThatAreValid + 1;
        }
        else {
            $('#linkThree').hide();
        }

        if (countOfLinksThatAreValid === 0) {
            $('#defaultLink').show();
        } else {
            $('#defaultLink').hide();
        }

        $('#Time').show();
        $('#Time').text(current);
    });


</script>
</head>
<body>
    <h1>Test Links</h1>
    <hr />
        <div id="linkOne" style="display:none">
        <p><a href="http://www.espn.com" target="_blank" title="ESPN.com"><b>ESPN.com</b></a></p>
        </div>

        <div id="linkTwo" style="display:none">
        <p><a href="http://www.cnnsi.com" target="_blank" title="Cnnsi.com"><b>Cnnsi.com</b></a></p>
        </div>

        <div id="linkThree" style="display:none">
        <p><a href="http://www.cbssports.com/" target="_blank" title="CBSSports.com"><b>CBSSports.com</b></a></p>
        </div>

        <div id="defaultLink" style="display:none">
        <p><a href="http://www.cnn.com" target="_blank" title="CNN.com"><b>CNN.com - Default Link</b></a></p>
        </div>

        <div id="Time" style="display:none">

        </div>
    <hr />
    <h1>End...</h1>

</body>
</html>
Duncan Revell

Duncan Revell 78 points
Registered Developer

I'm not sure javascript is needed for this.

Try date('Y-m-d')

Drew McLellan

Drew McLellan 2638 points
Perch Support

The date needs to be ISO format, so try date('Y-m-d H:i:s')