Get posts after today (upcoming events)

I’m using PHP Exec to build a widget to show a list of upcoming events:

<?php
function filter_whene($whene = '') {
    $whene .= " AND post_date >= '" . date('Y-m-d') . "' ";
return $whene;
  }
add_filter('posts_whene', 'filter_whene');
query_posts('cat=10&showposts=3&');
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <li><?php the_date(); ?> <a href="<?php the_permalink(); ?>">
  <?php the_title(); ?>
  </a>  </li>
<?php endwhile;
else: ?><p>Não há eventos agendados.</p>
<?php endif; ?>
<?php remove_filter('posts_whene', 'filter_whene'); ?>
<?php wp_reset_query(); ?>

It seems to work on the front end but I get:

Read More

Fatal error: Cannot redeclare filter_whene() (previously declared in
/home/content/a/c/a/acamorg/html/cea2/wp-content/plugins/exec-php/includes/runtime.php(42)
: eval()’d code:3) in
/home/content/a/c/a/acamorg/html/cea2/wp-content/plugins/exec-php/includes/runtime.php(42)
: eval()’d code on line 6

on the widget back-end.

Any suggestions?

Thank you.

Related posts

Leave a Reply

2 comments

  1. Hi @user2816:

    It would seem you are using that code in more than one widget? Try changing part of that code from these:

    function filter_whene($whene = '') {
      $whene .= " AND post_date >= '" . date('Y-m-d') . "' ";
      return $whene;
    }
    add_filter('posts_whene', 'filter_whene');
    

    To this:

    if (!function_exists('filter_whene')) {
      function filter_whene($whene = '') {
        $whene .= " AND post_date >= '" . date('Y-m-d') . "' ";
        return $whene;
      }
    }
    add_filter('posts_whene', 'filter_whene');
    

    Better, move as much of your code as possible to your theme’s functions.php file. Wrap it in a function and then you only have to type one line of code in your PHP Exec widget.

    Or better yet, get rid of that plugin completely and write your own widget; because PHP Exec is evil.

    P.S. Okay, PHP Exec is not evil per se, but once you try to do anything more than trivial it creates more problems than it solves.

  2. This doesn’t directly answered your question. I use this snippet on my sidebar, to get future posts:

    <ul>
    <?php
    query_posts('post_status=future&order=asc&orderby=date');
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
      echo "<li>";
      echo the_date();
      echo "<br />";
      echo the_title();
      echo"</li>";
    endwhile; else:
       echo "<li>there's no future post at the moment.</li>";
    endif;
    ?>
    </ul>