[Plugin: Mini Loops] Feature Patch: Ignore posts over X days old
-
Not sure if this is the right place to post — please let me know if it isn’t. I really like this plugin, but I want to show a list only of posts that are recent. So I patched the plugin to have a new filter to only show posts are at X days old or fewer. I haven’t done much testing, but its a pretty simple patch and it works for me. Here’s my patch:
Index: helpers.php =================================================================== --- helpers.php (revision 507433) +++ helpers.php (working copy) @@ -5,6 +5,7 @@ 'hide_title' => 0, 'title_url' => '', 'number_posts' => 3, + 'maximum_age' => 0, 'post_offset' => 0, 'post_type' => 'post', 'post_status' => 'publish', @@ -39,6 +40,7 @@ //since this function can be called in the template, re-escape the parameters $number_posts = (int) $number_posts; + $maximum_age = (int) $maximum_age; $post_offset = (int) $post_offset; $post_type = esc_attr( $post_type ); $post_status = esc_attr( $post_status ); @@ -123,6 +125,11 @@ 'post__not_in' => $exclude, ); $query = apply_filters( 'miniloops_query', $query ); + if ( $maximum_age != 0 ) { + $mindate = date("Y-m-d", time() - ($maximum_age * 24*60*60)); + $maximum_age_func = create_function('$filter','$mindate = date("Y-m-d",time() - ($maximum_age*24*60*60));$filter .= " AND post_date >= \'' . $mindate .'\'"; return $filter;'); + add_filter('posts_where',$maximum_age_func); + } //for testing //return ' <pre>'. print_r( $query, true ) .'</pre> '; @@ -132,6 +139,10 @@ if ( $reverse_order ) $miniloop->posts = array_reverse( $miniloop->posts ); if ( $shuffle_order ) shuffle( $miniloop->posts ); + if ( $maximum_age != 0 ) { + remove_filter('posts_where','filter_maximum_age'); + } + //for testing //return ' <pre>'. print_r( $miniloop, true ) .'</pre> '; Index: form.php =================================================================== --- form.php (revision 507433) +++ form.php (working copy) @@ -23,6 +23,12 @@ <input class="widefat" id="<?php echo $this->get_field_id('post_offset'); ?>" name="<?php echo $this->get_field_name('post_offset'); ?>" type="number" value="<?php echo $post_offset; ?>" /> </label> </p> + <p style="width:100%;float:left;"> + <label for="<?php echo $this->get_field_id('maximum_age'); ?>"><?php _e('Maximum Age of Posts (in days):', 'mini-loops' );?> + <input class="widefat" id="<?php echo $this->get_field_id('maximum_age'); ?>" name="<?php echo $this->get_field_name('maximum_age'); ?>" type="number" value="<?php echo $maximum_age; ?>" /> + </label> + </p> + <p style="width:48%;float:left;"> <label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e('Post Type:', 'mini-loops' );?> <select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>"> Index: widget.php =================================================================== --- widget.php (revision 507433) +++ widget.php (working copy) @@ -34,6 +34,7 @@ $instance['hide_title'] = (bool) $new_instance['hide_title'] ? 1 : 0; $instance['title_url'] = esc_url( $new_instance['title_url'] ); $instance['number_posts'] = (int) $new_instance['number_posts']; + $instance['maximum_age'] = (int) $new_instance['maximum_age']; $instance['post_offset'] = (int) $new_instance['post_offset']; $instance['post_type'] = esc_attr( $new_instance['post_type'] ); $instance['post_status'] = esc_attr( $new_instance['post_status'] ); @@ -108,6 +109,11 @@ <input class="widefat" id="<?php echo $this->get_field_id('number_posts'); ?>" name="<?php echo $this->get_field_name('number_posts'); ?>" type="number" value="<?php echo $number_posts; ?>" /> </label> </p> + <p style="width:100%;float:left;"> + <label for="<?php echo $this->get_field_id('maximum_age'); ?>"><?php _e('Maximum Age of Posts (in days):', 'mini-loops' );?> + <input class="widefat" id="<?php echo $this->get_field_id('maximum_age'); ?>" name="<?php echo $this->get_field_name('maximum_age'); ?>" type="number" value="<?php echo $maximum_age; ?>" /> + </label> + </p> <input name="<?php echo $this->get_field_name('post_offset'); ?>" type="hidden" value="<?php echo $post_offset; ?>" /> <input name="<?php echo $this->get_field_name('post_type'); ?>" type="hidden" value="<?php echo $post_type; ?>" /> <input name="<?php echo $this->get_field_name('post_status'); ?>" type="hidden" value="<?php echo $post_status; ?>" />
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘[Plugin: Mini Loops] Feature Patch: Ignore posts over X days old’ is closed to new replies.