Textpattern tips, tutorials and code snippets

Articles from matching days or months

I run a photoblog with daily entries going back 5 years. I wanted to have a way to display the entry posted on the same month/day for other years. So if someone was viewing a post from 10 February 2009, it would link to posts from 10 February 2008, 10 February 2010, etc. This uses Stef Dawson’s plugin smd_query to query the database for the other posts. It should be noted that all of the variables need to be lowercase in order for smd_query to properly read them.

Declare the variables for the time of the post being viewed

<txp:php>
global $variable;
$ts = posted(array('format' => '%s')); // UNIX timestamp of the current article
$variable['entry_year'] = strftime('%Y', $ts);
$variable['entry_month'] = strftime('%m', $ts);
$variable['entry_day'] = strftime('%d', $ts);
</txp:php>

A simple way to just print out a list of posts

<txp:smd_query query="SELECT ID, Title, Posted, YEAR(Posted) AS theYear
        FROM textpattern
        WHERE DATE_FORMAT(Posted, '%m') = '?entry_month'
        AND DATE_FORMAT(Posted, '%d') = '?entry_day'
        AND YEAR(Posted) != '?entry_year'
        ORDER BY Posted desc"
    wraptag="ul" break="li">
{theYear} : <txp:permlink id="{ID}">{Title}</txp:permalink>
</txp:smd_query>

I modified it a bit for my personal site to also display a thumbnail for the photo using data in Custom1:

<txp:smd_query query="SELECT ID, Title, Posted, Custom_1, YEAR(Posted) AS theYear
        FROM textpattern
        WHERE DATE_FORMAT(Posted, '%m') = '?entry_month'
        AND DATE_FORMAT(Posted, '%d') = '?entry_day'
        AND YEAR(Posted) != '?entry_year'
        ORDER BY Posted desc">
<div class="otherYears">
<txp:permlink id="{ID}">{theYear}</txp:permlink><br />
<txp:permlink id="{ID}"><img src="<txp:site_url />images/{Custom_1}-t.jpg" alt="<txp:title />" /></txp:permlink>
</div>
</txp:smd_query>

Thanks to Stef Dawson for his plugin and for helping me find the solution over on the smd_query forum thread.

Use Textile help to style your comments