logo

$20
How can I display the date created for my blog categories?

On my archives page beside each category, I want to display the date it was created. Ideally, it would be something like this, though I know it doesn't exist -

<?php wp_get_post_categories('date_created=') ?>

Any ideas?

Darren Hoyt | 12/07/09 at 4:42pm | Edit


(16) Possible Answers Submitted...

  • avatar
    Last edited:
    12/07/09
    5:05pm
    greggo says:

    GREAT IDEA!

    Here's what you do...

    Type this into your code


    And that's it!

    attachment image View Attachment

  • avatar
    Last edited:
    12/08/09
    12:29pm
    vsellis says:

    <?php the_time('F j, Y'); ?>

    Will display the date as ex: January, 4, 2010

    alternatively

    <div><?php the_time('M j, Y'); ?></div>

    would display the date as Jan 4, 1020

  • avatar
    Last edited:
    12/08/09
    1:02pm
    Dan Davies says:

    Without some modification, I'm not sure how one would return the date the category was created. It played on my mind to somehow include the datestamp of the first post within each category, but then I found this <a href="http://www.dagondesign.com/articles/recent-categories-plugin-for-wordpress/">recent categories plugin</a>. I hope that helps.

  • avatar
    Last edited:
    12/08/09
    3:40pm
    vsellis says:

    Darren, I should have read your question more carefully. I looked at the wordpress database and there is nothing in the terms/taxonomies tables that captures the date you create a category so I'm not sure there is a way to do what you are looking for out of the box. It sounds like the link Dan pointed you to is your best bet

  • avatar
    Last edited:
    12/08/09
    4:09pm
    deadlyhifi says:

    As the others have said the date of category creation is not stored in the the database.

    The easiest way to do it would be to type in the date in the description of the category, then output it using

    <?php echo category_description( $category ); ?>
    .
    See codex.wordpress.org/Template_Tags/category_description for more uses of this.

  • avatar
    Last edited:
    12/08/09
    4:53pm
    Jay Philips says:

    You could use this:

    <?php the_time('l, F jS, Y') ?> at 
    <?php the_time() ?>


    Which will display:
    Monday, February 12, 2003 at 11:32

    Or you could use this:
    <?php the_time('m/j/y g:i A') ?>


    Which will display:
    POSTED: 05/12/04 9:35 AM

  • avatar
    Last edited:
    12/08/09
    6:11pm
    badcat says:

    As mentioned above, since the date of the category creation is not captured in the DB, might it not make sense to use the date of the first post added to that category? I guess it really depends on what the intent is here. If truly to use on an archives page, then the first post in that cat would probably suffice.

    However in a situation where there are many editors, contributors and such - seeing when a particular category (ie a possible similarly named duplicate) was created might be helpful in working out editorial kinks.

  • avatar
    Last edited:
    12/10/09
    1:25pm
    Japh says:

    It would be quite doable to make a small plugin to make this information available for you.

    You could create a separate database table with a category's created and modified dates and use the appropriate actions.

    for example:


    function cat_add($CatID) {
    // Code here to add the created date to your plugin DB table
    }

    add_action('create_category','cat_add');

    function cat_change($CatID) {
    // Code here to update the modified date to your plugin DB table
    }

    add_action('edit_category','cat_change');

    function cat_remove($CatID) {
    // Code here to delete the category from your plugin DB table
    }

    add_action('delete_category','cat_remove');


    I could create this plugin for you if you wish. I think actually creating the plugin is probably beyond the scope of this question ;)

    Hope that helps!

  • avatar
    Last edited:
    12/08/09
    10:39pm
    steve says:

    I think it makes the most sense to use the category description field to set the date you're looking for and then pull the information using:

    <?php echo category_description( $category ); ?>

    As described above.

  • avatar
    Last edited:
    12/09/09
    12:45am
    Japh says:

    If you did want to do it by putting the value into the category's description, place the following snippet in your functions.php:


    function save_category_created($catID) {
    $cat = (array) get_category_to_edit($catID);
    $cat['category_description'] = date('U'); // Here you can format the date as you like. I put it as the timestamp for easy conversion when used.
    wp_update_category($cat);
    }

    add_action('create_category', 'save_category_created');


    Then, you can just fetch the category description wherever you need it.

  • avatar
    Last edited:
    12/09/09
    2:20pm
    spivurno says:

    Japh's concept of a simple plugin to handle this functionality is by far the most ideal solution; it requires no additional user interaction and could be easily modified to give you any category meta date you might want in the future.

    Entering the date in the description field requires an additional level of user interaction for every category and deprives you of the intended use of that field. Not a horrible solution, but not ideal.

  • avatar
    Last edited:
    12/10/09
    12:06am
    Lew Ayotte says:

    Yeah, WordPress doesn't keep track of the date-time of category creation. The only two options would be to use the description field (as described above) or to add/modify a table and hook into the function that adds categories to include the date.

    I had a similar need for some work I was doing a few weeks ago. The "date" wasn't necessary, but ordering the category by "date" was. I was able to achieve a similar result by ordering by category ID. Since the category IDs always increment then the "oldest" categories would have the lowest numbers.

    Of course this wasn't exactly described as your need, but I thought I'd throw it in there.

  • avatar
    Last edited:
    12/10/09
    2:39am
    Gerasimos Tsiamalos says:

    I think Japh's solution is the best. Although he mentions something about using a different db table? Wouldn't the options table do the job?

  • avatar
    Last edited:
    12/10/09
    2:56am
    Japh says:

    I think using the options table would be a little cumbersome, especially with more than a few categories. Using a separate table would allow you to essentially extend the attributes of a category with your own custom ones.

    Normally you might add this sort of information into the 'wp_terms_taxonomy' table, but it's best not to mess with core WP stuff, or you might ruin things for automatic upgrades etc (either WP, or your plugin might break).

    So I think an extra table would be a more robust option if you were to develop a plugin for this.

  • avatar
    Last edited:
    12/10/09
    7:14am
    Gerasimos Tsiamalos says:

    @Japh:

    Indeed. I just thought first that using an extra table would be unnecessary but on a second thought it makes sense. Someone could create more attributes related to the category so yeah. Good point :)

  • avatar
    Last edited:
    12/10/09
    1:22pm
    Max says:

    I think that retrieving the date of the oldest post in a category might work fine.

    Here is the code:


    <?php

    $args = array('orderby' => 'name', 'order' => 'ASC');

    $categories = get_categories($args);

    foreach($categories as $category)
    {
    $my_query = new WP_Query('category_id='
    . $category->cat_ID
    . '&showposts=1&orderby=date&order=DESC');

    while($my_query->have_posts()) : $my_query->the_post();
    echo $category->name . ' '
    . get_the_time('d/m/y') . '<br />';
    endwhile;
    }
    ?>

This question has expired.





Current status of this question: Completed