• Hi there,
    I have a bit delicate problem I can’t solve myself for hours…

    Here is what I’m trying to do:
    I want to display an upcoming events in local restaurant, whole output is heavily customized (by CSS) so I can’t use any Events plugin.

    The idea is simple, display posts referring to today’s or future event and hide (move to the Archive category) older events (yesterday and older). But here is the problem… posts are published long before event actually happens so that means I can’t filter posts by using query_posts with time parameters.

    Only idea which came up to my mind is filtering by custom field value. While entering a posts (event) you specify current date (let’s say 18.12.2009 – tomorrow). And some kind of script will check the current date and date in custom field. If the date in custom field is older that post won’t shows up and vice versa. The archive page do it reversed, display posts older that today’s date.

    But the problem is I have no idea how to compare custom fields with today’s date in WordPress template file. Please, does anyone have an idea how to do this? I’m also opened to any other suggestions that will lead to success :).

    Thanks a lot…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Not sure what you want the final outcome to look like or what you want it to include but here’s a start:

    <?php
    $datevar = date('Y-m-d');
    $posts = "SELECT * from $wpdb->posts WHERE post_date >= '".$datevar."' AND post_type='post' ORDER BY post_date";
    $postresult = mysql_query($posts) or die(mysql_error());
    echo "<ul>";
    while ($row = mysql_fetch_array($postresult)) {
    $cont = $row[post_content];
    $cont = preg_replace("/<img[^>]+\>/i", "", $cont);
    $excerpt = substr($cont,0,300);
    echo "<li><h3><a href=\"{$row[guid]}\">{$row[post_title]}</a></h3>{$excerpt}[...]</li>";
    }
    echo "</ul>";
    ?>

    This will create an unordered list of all posts dated today or in the future. It will link the title to the post. It will remove any images in the output and create an excerpt of 300 characters followed by “[…]”.

    It’s a start I suppose.

    Another suggestion:

    You could always schedule your posts for future publishing within WordPress and use the following code on your site to list it:

    <?php query_posts('showposts=10&post_status=future'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<h2><?php the_title(); ?></h2>
    		<span class="datetime"><?php the_time('j. F Y'); ?></span></p>
    <?php endwhile;
    else: ?><p>No future events scheduled.</p>
    <?php endif; ?>

    Code from https://www.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/

    Thread Starter Jakub Machala

    (@machi)

    Wow, I don’t know how to thank you both!

    tugbucket: This is a bit more complicated solution and am I right if I say that this solution doesn’t allow me to set other “criteria” such as category, order_by,…?

    geraldyeo: Where did you find it :)? It’s completely missing in the codex and it solves my entire problem! I can easily filter posts by published/unpublished, amazing. You saved my day!

    Thanks to you both guys…

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filtering posts by custom fields values’ is closed to new replies.