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.

Show Latest Category Added, Even if Empty

April 14, 2009 5:58pm

Subscribe [2]
  • #1 / Apr 14, 2009 5:58pm

    davidrussell

    102 posts

    I’ve done the research and can’t seem to find anyone with this same scenario. Here it is: I use categories in one weblog and use the custom category fields to store lots of data about the category. I’d like to have one page that shows only the most recently added category, even if that category has no entries. (And also if that category has entries. Essentially, always show the most recent category created.)

    I can work the exp:weblog:entries and {categories} tags to get the most recent category show “by weblog” but that won’t work with require_entry=“no” for some reason. Perhaps I am going about this the wrong way.

    Any ideas? Seems like I am missing something simple. :| I know I can specify the category_id, but I want to go the more dynamic route, if possible.

  • #2 / Apr 14, 2009 6:07pm

    Ingmar

    29245 posts

    I’d suggest a simple SQL query. Something like this, perhaps:

    SELECT cat_name, cat_id FROM `exp_categories` 
    WHERE site_id = "1" AND group_id = "1" ORDER BY cat_id DESC LIMIT 1

    That should always get you the very latest category. Makes sense? Moving to Howto.

  • #3 / Apr 14, 2009 6:29pm

    davidrussell

    102 posts

    Okay, I have this solution in place. But I think it’s going to get a little complex. Part of the reason I’m going this route is to have a page list the meta information I have in the category custom fields. That creates a much broader SQL query, which can be done, but doesn’t seem as elegant as doing something natively in EE or with an extension. Then again, I suppose it’s the same lifting that would be done by the system regardless of where the code is processed. 😊

    I am beginning to wonder if I should have created a weblog for this information and just used related entries. Thoughts? Should I just create the complex SQL query? Or re-think my approach?

    Thanks so much Ingmar. You rock dude.

  • #4 / Apr 14, 2009 6:54pm

    davidrussell

    102 posts

    Actually, I think I have this fairly well knocked out. I’m a bit noobish on SQL and the exp:query tag, but here is what I came up with that works. Unfortunately, it’s not elegant enough to use the custom field names, but I’m only setting this up once and letting it roll, so I think it will suffice:

    {exp:query sql="SELECT * FROM `exp_categories` WHERE site_id = '1' AND group_id = '1' ORDER BY cat_id DESC LIMIT 1"}{embed="includes/header"}
        
        {exp:query sql="SELECT * FROM `exp_category_field_data` WHERE cat_id = '{cat_id}' LIMIT 1"}<div class="page">
            
            <h2>{cat_name}</h2>
    <p>        </p><h3>{field_id_2}</h3>
    <p>        <br />
            {cat_description}<br />
        <br />
        </div>{/exp:query}</p>
    
    <p>{embed="includes/footer"}{/exp:query}

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

ExpressionEngine News!

#eecms, #events, #releases