Support » Fixing WordPress » if < todays_date = date from custom field > do stuff

  • Resolved belalugosi

    (@belalugosi)


    Can somebody please help me with this code?
    //<?php query_posts(‘category_name=events’);?>
    <?php $todays_date = date(“Y-m-d”); if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php $event = get_post_meta($post->ID, ‘event’, $single = true); ?>
    <?php $start_date = strtotime($event); ?>
    <?php $today = strtotime($todays_date); ?>

    <?php if(“$start_date = $today”) { ?>

    <?php the_title(); ?>

    <? } ?>

    <?php endwhile; else: ?>
    <p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
    <?php endif; ?>//

    I have a custom field labeled “event” which has date values of this type 2008-08-09. On home page of my blog i only want to show posts that have the “event” custom field date value equal with the current server date value. With the above code every post from the event’s category shows up. Any help is very much appreciated.

    here is my test page: http://viku.aseptic.ro

Viewing 5 replies - 1 through 5 (of 5 total)
  • If your event field already has things in the format 2008-08-09, don’t change things back to time. Just compare strings.

    Try this:

    <?php query_posts('category_name=events');
    $todays_date = date("Y-m-d"); if (have_posts()) : while (have_posts()) : the_post();
    
    $event = get_post_meta($post->ID, 'event', $single = true);
    
    if($event == $todays_date) {
    the_title();
    }
    
    endwhile; else: ?>
    
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    That may work.

    Thread Starter belalugosi

    (@belalugosi)

    Thank you very much for clearing this out for me. <3

    My pleasure! 🙂

    Cheers,
    A

    Thread Starter belalugosi

    (@belalugosi)

    Is this safe to use? I mean.. if you have a thousand posts or so.

    Anyone know how to only list 3 of the posts with similar variables? I got this now but it doesn’t quite work since it only get 3 posts and then filters them.. i need to get the last posts from my custom value and then choose the latest 3 from that…

    <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; ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘if < todays_date = date from custom field > do stuff’ is closed to new replies.