• Good day,

    I’m doing an events calender and need to dynamically exclude event posts that are older then TODAY via a custom value (not the actual wordpress post date, the custom value date).

    Any ideas? My code so far…:

    <h2>Upcoming Events</h2>
    <?php
    $lastposts = get_posts('category_name=events&meta_key=edate&orderby=meta_value&numberposts=3&order=asc');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>
    
    <div class="newsitem" onclick="location.href='<?php the_permalink() ?>';">
    <div class="overlay" title="Click to read more about this event."></div>
    <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    <p>Date: <?php $key="edate"; if(get_post_meta($post->ID, $key, true)){ echo get_post_meta($post->ID, $key, true); } ?><br />Location: <?php $key="elocation"; if(get_post_meta($post->ID, $key, true)){ echo get_post_meta($post->ID, $key, true); } ?></p>
    </div>
    
    <?php endforeach; ?>

    I guess i’m in need of some ‘if’ + ‘AND’ to get posts only between *NOW* and the future (like year 2097 or something 🙂 …)

    But how….? My date format for the custom field is YEAR-MOTH-DAY, i.e. 2009-10-31.

Viewing 1 replies (of 1 total)
  • Thread Starter Tommie Hansen

    (@tommiehansen)

    I solved it myself with a count-value! Now it works great and without any bloated plugin or something. 🙂

    <?php
    $lastposts = get_posts('category_name=kalender&meta_key=kdatum&orderby=meta_value&order=asc');
      $count = 0;
    	foreach($lastposts as $post) :
    	  if ($count >= 3) {
    	    break;
    	  }
    		setup_postdata($post);
    		//check the dates
    		$todays_date = date("Y-m-d", strtotime('-1 day'));
    		$kdatum = get_post_meta($post->ID, 'kdatum', $single = true);
    		if(strtotime($kdatum) > strtotime($todays_date)) {
    			?>
    			<div class="newsitem" onclick="location.href='<?php the_permalink() ?>';">
    			<div class="overlay" title="Klicka för att läsa mer"></div>
    			<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    			<p>Datum: <?php $key="kdatum"; if(get_post_meta($post->ID, $key, true)){ echo get_post_meta($post->ID, $key, true); } ?><br />Plats: <?php $key="kplats"; if(get_post_meta($post->ID, $key, true)){ echo get_post_meta($post->ID, $key, true); } ?></p>
    			</div>
    			<?php
    			$count += 1;
    		} //end date check ?>
    	<?php endforeach; ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Exclude old posts before “TODAY”’ is closed to new replies.