ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Select entries from a channel, just the last for each category

June 24, 2010 8:16am

Subscribe [2]
  • #1 / Jun 24, 2010 8:16am

    giusi

    94 posts

    Hi,

    I have a channel with a category group associated, each category is a mini-blog, I need to show the last 12 entries from the channel, but just the last from each mini blog. Additionally, I need the first 4 enclosed in a div, the the following 4 and so on, due to a javascript effect i have to apply. Maybe I need the exp:query module with some custom query but I am not good with SQL. Anyone has some idea to solve my problem, or has solved this before (it seems to me as a common task to accomplish on websites).

    Thanks in advance.

  • #2 / Jun 24, 2010 1:05pm

    lebisol

    2234 posts

    Using weblog parameters ‘sort and order by date’ and ‘limit’ and select specific categories.

    {exp:channel:entries  channel="news" orderby="date" sort="asc" limit="12" category="2|45|4|9"}
    {title}
    {body}
    ...
    {/exp:channel:entries}

    for second part look into ‘offset’ (offset 4 then offset 8 then offset 12 for each occurrence of channel tag)

    or using ‘range’ (from 1 to 4, from 4 to 8…for each occurrence of channel tag)

  • #3 / Jun 24, 2010 1:20pm

    giusi

    94 posts

    I see I haven’t been clear in my first post! Apologies… your tip is clever but doing it this way the template would need to be updated when a new category is created, what I would need is a way to get all the categories from the category group. But now that I think about that, maybe using the category_group=”” parameter I can solve the issue.

    Thank you very much for your invaluable help 😊

  • #4 / Jun 24, 2010 1:30pm

    lebisol

    2234 posts

    Yap, category group would pull all of categories in it, just organize them in order that makes most sense for your content.

  • #5 / Jun 24, 2010 1:32pm

    giusi

    94 posts

    Thought a bit more about that, and I am not sure that this way I will retrieve only the last entry from each category. Say there are 1 new entry in each category but 2 in another, won’t I have 2 post from that one?

  • #6 / Jun 24, 2010 1:52pm

    lebisol

    2234 posts

    Not from each category but rather a channel (as you requested)....the date or “latest” refers to entries entry date - regardless of what category they belong.
    Your “latest” entries in categoryA can be 1-2 months old while categoryB might have 10 entries in last 2 days….and how many would you want to pull from each category? The count of 12 entries and ‘latest’ becomes convoluted…12 total entries or latest entry from total of 12 categories? something to think about…
    For this, you would need to use a query module and sql.

  • #7 / Jun 24, 2010 2:07pm

    giusi

    94 posts

    What i need is : last 12 entries from a channel, only one for each a category (when categories will be more than 12 only latest 12 entries will be showed). Yes me to thinks I will need the query module. Sadly I suck at sql : )

  • #8 / Jun 24, 2010 6:56pm

    lebisol

    2234 posts

    ok, what happens when there is 13, 20 categories?
    what if not every category has an entry?

  • #9 / Jun 24, 2010 11:26pm

    ender

    1644 posts

    assuming I’m reading this right, this query should work… latest 12 entries where each is from a different category.

    note that this assumes that entries with higher IDs are newer than entries with lower IDs.  This should be true unless you manipulate your blog entry dates.  It is possible to not fudge on this but it’s a more complicated query and I don’t have time or inclination to write it at the moment 😉

    SELECT
        t.entry_id,
        t.title,
        t.url_title,
        t.entry_date,
        c.cat_id,
        c.cat_name,
        c.cat_url_title,
        d.field_id_XX AS my_custom_field -- use the proper IDs and aliases for your custom fields
    FROM
        exp_weblog_titles AS t NATURAL JOIN
        exp_weblog_data AS d NATURAL JOIN
        exp_category_posts AS p JOIN
        exp_categories AS c ON p.cat_id = c.cat_id LEFT JOIN
        exp_category_posts AS p2 ON p.cat_id = p2.cat_id AND p.entry_id < p2.entry_id
    WHERE
        p2.entry_id IS NULL AND
        t.weblog_id = '1' -- use the proper weblog_id here
    ORDER BY
        t.entry_date DESC
    LIMIT 12;
  • #10 / Jun 25, 2010 7:23am

    giusi

    94 posts

    Thank you very much! This is more than enough 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases