Support » Themes and Templates » Display Post for Category within Last 90 Minutes

  • Resolved gbrown88

    (@gbrown88)


    Hey,

    I’m looking for some help with a custom query loop. I am trying to display posts for a certain category that where posted within that last 90 minutes. The script I’ve written is working, but it knocks out all the other loops and queries on the page. Here’s what I have:

    <?php
    global $post;
    function filter_where( $where = ” ) {
    // posts in the last 90 minutes
    $where .= ” AND post_date > ‘” . date(‘Y-m-d’, strtotime(‘-90 minutes’)) . “‘”;
    return $where;
    }
    add_filter( ‘posts_where’, ‘filter_where’ );

    $query = new WP_Query( $query_string.’&cat=979′ );
    while ($query->have_posts()) : $query->the_post(); ?>
    Hi
    <?php endwhile; wp_reset_query(); ?>

    Any help would be great, as I hope to do this in a couple more places.

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • try to remove the filter after your loop;

    example:

    <?php
    global $post;
    ...
    ...->the_post(); ?>
    Hi
    <?php endwhile; wp_reset_query(); ?>
    
    <?php remove_filter( 'posts_where', 'filter_where' ); ?>

    http://codex.wordpress.org/Function_Reference/remove_filter

    Thread Starter gbrown88

    (@gbrown88)

    Perfect! Thanks

    Thread Starter gbrown88

    (@gbrown88)

    Alright, so that worked and allowed the rest of the loops on the page to work properly, but when I use that twice on the same page, it doesn’t work.

    Any suggestions? Thans

    export the function to functions.php of your theme;

    function filter_where( $where = '' ) {
    global $post;
    // posts in the last 90 minutes
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-90 minutes')) . "'";
    return $where;
    }

    and only use the:
    <?php add_filter( 'posts_where', 'filter_where' ); ?>
    before the loop;
    and the:
    <?php remove_filter( 'posts_where', 'filter_where' ); ?>
    after the loop.

    Thread Starter gbrown88

    (@gbrown88)

    Alright, what if I want to pass different time intervals though throughout the page?

    Sometimes stuff from last 90 minutes, sometimes from last week, etc.

    right now i can only think of:

    – either make a different function for each time interval;

    – or use a global variable which you set before the add_filter line, to set the time interval:

    in your loop:

    <?php global $time_interval;
    $time_interval = 90; //time interval set in minutes
    add_filter( 'posts_where', 'filter_where' ); ?>

    and in your function:

    function filter_where( $where = '' ) {
    global $post; global $time_interval;
    $interval = isset($time_interval) ? $time_interval : 90;
    // posts in the last 'interval' minutes
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-'.$interval.' minutes')) . "'";
    return $where;
    }

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display Post for Category within Last 90 Minutes’ is closed to new replies.