Forums

Get posts from the last 60 minutes from a given category? (14 posts)

  1. elcarpo
    Member
    Posted 2 years ago #

    Is there a way to retrieve only one post from the last 60 minutes from a given category? I know that it can be done for the day, but what a bout an specific lapse of time, not days?

    thanks

  2. alchymyth
    The Sweeper
    Posted 2 years ago #

    i suppose you have read this already, if not here is the codex:
    http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters

    there might be a simple adaptation to change the given examples to one hour.

  3. elcarpo
    Member
    Posted 2 years ago #

    alchymyth, yep, i've seen it, but this

    <?php
      function filter_where($where = '') {
        $where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-60 minutes')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string . '&showposts=1&cat=72');
    ?>

    doesn't work (i mean, it will break all other loops)...

    thanks

  4. alchymyth
    The Sweeper
    Posted 2 years ago #

    i think you need to remove the filter after the query, so that the following loops are not effected.

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    You don't need the query_posts line ...
    Just add this to your theme's functions.php ..

    <?php
    function filter_where($where = '') {
    	if( !is_admin() && is_category('ID_OR_NAME_HERE') ) {
    		$where .= " AND post_date > '" . date('Y-m-d', strtotime('-60 minutes')) . "'";
    	}
    	return $where;
    }
    add_filter('posts_where', 'filter_where');
    ?>
  6. Mark / t31os
    Moderator
    Posted 2 years ago #

    Should of tested the above code more thoroughly before posting, lol..

    Forget what i said above..

    This works.. :) tested it down to the minute..

    <?php
    function filter_where($where = '') {
    	if( !is_admin() && is_category('ID_OR_NAME_HERE') ) {
    		$where .= " AND DATE_SUB(NOW(),INTERVAL 30 MINUTE) <= post_date";
    	}
    	return $where;
    }
    add_filter('posts_where', 'filter_where');
    ?>

    Hours and minutes will both work.. (examples for modification to above)

    //$where .= " AND DATE_SUB(NOW(),INTERVAL 2 HOUR) <= post_date";
    //$where .= " AND DATE_SUB(NOW(),INTERVAL 1 HOUR) <= post_date";
    //$where .= " AND DATE_SUB(NOW(),INTERVAL 30 MINUTE) <= post_date";
    //$where .= " AND DATE_SUB(NOW(),INTERVAL 45 MINUTE) <= post_date";


    NOTE:
    This is done using MySQL functions, the naming scheme for minutes and hours is minus plurals, so it's not minutes, but minute, whether you use 1, 5 or 10... (same goes for hours)

    The approach was taken from the MySQL docs page for 5.1, if you have problems let me know..

    Refs:
    http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters
    http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

  7. elcarpo
    Member
    Posted 2 years ago #

    i'll try and see how it goes!

    thanks!

  8. elcarpo
    Member
    Posted 2 years ago #

    t31os_, sorry for being such a noob, but how do i apply this code?

    i mean, how do i call that query to display the posts?

    thanks

  9. Mark / t31os
    Moderator
    Posted 2 years ago #

    Add it into your theme's functions.php file (taking care with PHP tags).

    Be sure to update the category name or ID...

    Then when you query for that category, the function will kick in and apply the date/time filter for you.

  10. elcarpo
    Member
    Posted 2 years ago #

    ok, it worked!

    i had to reset the query after the loop, but that did it...

    only thing (i supposed it could happen, but i wasn't sure) is that it won't show any posts when i click on the category (if posts are older than the time i set)

    thanks!!

  11. Mark / t31os
    Moderator
    Posted 2 years ago #

    Add more conditions to the function provided..

    If you need help doing so, then please tell me specifically which conditions will be true when you want the filter to run.

    Alternateively you could pull in posts from that catgegory in a page template, limiting it's usage to that page alone by then using an is_page() check in the place of the is_category() check..

  12. elcarpo
    Member
    Posted 2 years ago #

    actually, i can live with that

    but, just take into account that if i were to perform a search for that given category (if i click on the category link), posts won't appear...

    thanks!

  13. Mark / t31os
    Moderator
    Posted 2 years ago #

    :)

    Add more conditions if you like.... the important thing is, that it works... :)

  14. lwoodward108
    Member
    Posted 2 years ago #

    You all smell like overheated computer chips

Topic Closed

This topic has been closed to new replies.

About this Topic