FOWD, EE and a little tip

One of the main reasons why I went to this year’s Future Of Web Design conference, was to attend Jamie Pittock’s workshop on ExpressionEngine. As a freelancer, I only see my own EE implementations, so I jumped at the opportunity to see how others use it to deploy sites. The workshop turned out to be both fun and inspirational — path.php will never be the same again. Kudos to Jamie for putting it together.

The second workshop I attended was about JavaScript by Stuart Langridge. Between the jokes and the put-downs, Stuart had his class do a couple of exercises. I managed to help out my neighbour a bit, who turned out to be an EE enthusiast herself and had recently used one of my add-ons.

I promised Emily I’d write a blog post about a little trick I sometimes use to retrieve certain entries from the database when the default weblog:entries tag won’t let you. In her case, she needed to get all entries from a given month, regardless of the year. She ended up using categories, which is fine, but here’s an approach you could also use.

Check the database

Weblog entries are stored in the table exp_weblog_titles. In that table you’ll find values for year, month and day. This means we can filter entries per month, regardless of the year, albeit not with regular EE tags. We’ll need to get them manually.

Set up a template, use PHP

Create a template (for example: archives/month) and turn on PHP on input. We’re going to use PHP to fetch all entry ids for any given month. In this case, a numeric value of the month will be available in {segment_3}: http://domain.com/archives/month/5. The PHP will look something like this:

<?php
global $IN, $DB;

// get database-safe segment
$seg3 = $DB->escape_str($IN->fetch_uri_segment(3));

// query to get all entry_ids for a given month
$query = $DB->query("SELECT entry_id FROM exp_weblog_titles WHERE month = '{$seg3}'");

// initiate ids array
$ids = array();

// loop through results, populate array
foreach ($query->result AS $row):
	$ids[] = $row['entry_id'];
endforeach;
?>

Use IDs as input for {exp:weblog:entries}

You now have an array filled with all the right entry_ids. You only need to display them with the {exp:weblog:entries} tag, using the entry_id parameter to limit the entries to the ones you’ve just retrieved. That would look something like this:

{exp:weblog:entries weblog="articles" entry_id="<?=implode('|', $ids)?>" dynamic="off" disable="pagination"}
	[...]
{/exp:weblog:entries}

You could add further filtering, like statuses or categories, but all the entries will be from the given month, because of the entry_id parameter.

Remember dynamic=”off”

I’ve used this approach on many occasions. Use PHP to get the right ids first, then use them as input for your regular EE tags. It works like a charm, but has its down side as well. Because you’re using the segments in custom way, you’ll need to set dynamic="off". This will disable the use of pagination. That might be a bad thing, or on the other hand, it might not matter at all.

May 5th 2009 | Conferences, ExpressionEngine, PHP | 6 comments

Comments

  1. 1 Erwin Heiser May 5th 2009, 11:33

    Sweet!
    Always good to see how others implement some of the not-so-standard stuff :)
    Coming to Leiden in October? http://www.eeci2009.com/

  2. 2 Low May 5th 2009, 11:37

    @Erwin: I live there, so yes, most definitely!

  3. 3 Emily May 29th 2009, 19:34

    Hi Low! It was very cool meeting you at FOWD09 and thanks so much for posting this tip. I decided to implement your solution because my work-around of having the website editor select a month as a category was such a bore, and I wanted to make sure I could get this to work (who knows when I’ll need it again!).

    I thought it might be interesting to share the example I used it for here, as it is quite an unusual case. I wanted to create an archive of past entries that is grouped by month, regardless which year they were posted. Why? Because this blog is about seasonal/month specific topics which you might want to browse by month, but wouldn’t need to browse by year.

    Unfortunately the link I’m providing here doesn’t demonstrate the results too well because we are in the first year of publishing, so there is only one post per year. But I have tested it and I assure you it works, with Low’s code used exactly as above.

    The one variation I would have liked to make to this code was to be able to have the month URL indicated by name not numbers. So I wanted
    what-to-look-for-in-the-woods/month/april/
    not
    what-to-look-for-in-the-woods/month/04/

    Unfortunately I didn’t have the time to figure out how to integrate a month name to number transformation into this solution. If anyone else has done this I’d love to see it.

    One other point is you will see I have list of months on the side - this is done using Low’s Yearly Archives plugin, and I am going to add a comment there with a quick explanation of how I got only the months -not years- showing up here.

    Cheers Low!

  4. 4 Cem Meric August 5th 2009, 08:40

    Good tip Low.

    Emily, also if you can’t use PHP in your templates for some reason try below code logic to suit your url base i.e. “what-to-look-for-in-the-woods/month/april/”

    {if segment_3 == 'may'}
    {exp:query sql='SELECT entry_id FROM exp_weblog_titles WHERE month = 5'}
    {exp:weblog:entries weblog="media" entry_id="{entry_id}" dynamic="off"}
    {title}
    {/exp:weblog:entries}
    {/exp:query}
    {/if}

    You would obviously build on this, perhaps use embeds to do “month name to number” conversion which will get rid of “if” checks

  5. 5 Low August 5th 2009, 10:55

    Completely forgot, but you can alter the above example to use month names instead quite easily. Above the query, add this:

    $months = array('january','february','march',...,'december');
    $month_num = array_search($seg3, $months) + 1;

    Don’t forget to change '{$seg3}' to '{$month_num}' in the query itself.

  6. 6 Low August 5th 2009, 11:01

    Oh, and Cem, nesting the Query and Weblog tags like that could result in LOTS of sql queries: for each found entry_id, a new weblog:entries tag is executed (or embed is parsed). A big performance hit. Would not recommend that route.

Leave a comment



Some html (a, em, strong, etc) allowed.
Email won’t be displayed and takes care of your Gravatar.