ThemeShaper Forums » Thematic

[closed]

Only return posts from the last 30 days

(6 posts)
  • Started 4 years ago by BAC
  • Latest reply from BAC
  • This topic is resolved
  1. BAC
    Member

    Hey.

    I tried to implement the code at codex into my child theme functions.php to only return posts from the last 30 days, include search results to no avail. (I just pasted it into the functions file)

    <?php
    //based on Austin Matzko's code from wp-hackers email list
    function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>

    Also, does anyone know how to post a message above a single post page when it is over 30 days old.. ie if someone lands from a search engine they get a message saying 'this post is out of date, try these posts' sort of thing?

    Thanks for any help on this.

    BAC

    Posted 4 years ago #
  2. Hi BAC. This code is based off a post at Wp-recipes: http://www.wprecipes.com/wordpress-hack-how-to-display-ads-on-old-posts-only I just tested it on my site and it works.

    function is_old_post($post_id=null){
    	$days = 30;
    	global $wp_query;
    	if(is_single() || is_page()) {
    		if(!$post_id) {
    			$post_id = $wp_query->post->ID;
    		}
    	$current_date = time();
    	$offset = $days *60*60*24;
    	$post_id = get_post($post_id);
    	$post_date = mysql2date('U',$post_id->post_date);
    	$cunning_math = $post_date + $offset;
    	$test = $current_date - $cunning_math;
    		if($test > 0){
    			$return = true;
    		}else{
    			$return = false;
    		}
    		}else{
    		$return = false;
    		}
    	return $return;
    }
    
    // The Single Post
    function child_single_post() { ?>
    			<div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
    <?php if(is_old_post()){ ?>
    <p>This post was published more than 30 days ago.</p>
    <?php } ?>
        			<?php thematic_postheader(); ?>
    				<div class="entry-content">
    <?php thematic_content(); ?>
    
    					<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    				</div>
    				<?php thematic_postfooter(); ?>
    			</div><!-- .post -->
    <?php
    }
    add_filter('thematic_singlepost', 'child_single_post');
    Posted 4 years ago #
  3. BAC
    Member

    @Devin

    Thanks so much for this, awesome. works a treat :)

    After much searching, I'm still struggling on how to filter the main loop and category loop to only return posts from the last 30 days...

    I need to add the filter mentioned in my original post above, directly above query_posts(). Can anyone point me in the right direction to achieve this?

    Thanks again.

    BAC

    Posted 4 years ago #
  4. BAC
    Member

    hi,

    I am still struggling with filtering the main and category loops to only return posts from the last x days.

    This is where I got to but it doesn't seem to be working for me.. Anyone know what I am doing wrong?

    Not sure if I can have a filter within an action like this?


    function x-days() {
    //based on Austin Matzko's code from wp-hackers email list
    function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);

    }
    add_action('thematic_above_indexloop', 'x-days');

    Posted 4 years ago #
  5. BAC
    Member

    Actually I think I am closer with the following code, however this returns 0 posts??

    // Filter Index Loop
    function remove_thematic_index_loop() {
          remove_action ('thematic_indexloop','thematic_index_loop');
    }
    add_action('init','remove_thematic_index_loop');
    
    function my_index_loop() {
    
    function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    
     while (have_posts()) : the_post(); ?>
    
              <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
                  <?php thematic_postheader(); ?>
                  <div class="entry-content">
    <?php thematic_content(); ?>
    				</div>
                  <?php thematic_postfooter(); ?>
              </div><!-- .post -->
    
          <?php endwhile;
    }
    add_action('thematic_indexloop', 'my_index_loop');

    I tried a simple

    query_posts('posts_per_page=2');

    instead of

    function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);

    And that works and displays only 2 posts so it seems i am on the right track maybe??

    Thanks for any help on this.

    BAC

    Posted 4 years ago #
  6. BAC
    Member

    Just in case anyone finds this useful, I solved this by installing the Auto Delete Posts plugin which has the option to move posts over x days to a new category rather than delete.

    I could then place the following code in my main loop just below have_posts with 20 being the category ID

    <?php if (in_category('20')) continue; ?>
    ie

    while (have_posts()) : the_post(); ?>
    <?php if (in_category('20')) continue; ?>

    I also changed Search Unleashed to exclude this category.

    Hope this helps.

    BAC

    Posted 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.