WordPress.org

Ready to get started?Download WordPress

Forums

The Events Calendar
Upcoming Events Query (3 posts)

  1. haydenhancock
    Member
    Posted 3 years ago #

    Hey everyone!

    I was messing around with calling a custom WP Query on the events to create a simple list of upcoming events on my front page. I realize I could use the widget but I didn't see the need to create a new widget area in this application. Basically, I just wanted to share my experiences with getting it to work.

    Here is what my code accomplishes.

    1. Creates a custom query from my "events" category (category id is 7 in my case). Also orders the events in ascending order.
    2. Compares today's date with the events end date to filter out past events.
    3. Displays the events events title and start date.

    Here is my code.

    <?php $custom = new WP_Query('cat=7&order=ASC'); ?>
    <?php if ($custom->have_posts()) { ?>
    	<?php while($custom->have_posts()) { ?>
    		<?php $custom->the_post(); ?>
    		<?php
    			// Comparing dates to filter out past events
    			$exp_date = the_event_end_date();
    			$todays_date = date("F j, Y");
    		?>
    		<?php if (strtotime($todays_date) >= strtotime($exp_date)) { ?>
    		<?php } else { ?>
    			<div id="article-<?php the_ID(); ?>" class="article">
    				<p class="date">Event Date: <?php echo the_event_start_date(); ?></p>
    				<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    				<p></p>
    			</div>
    		<?php } ?>
    	<?php } ?>
    <?php } else { ?>
    	<p>Sorry, there are no events.</p>
    <?php } ?>

    I realize that there is probably a much better way to do this (I am a newbie with PHP) so please make adjustments if necessary.

  2. red321
    Member
    Posted 3 years ago #

    Take a look at the FAQ
    http://wordpress.org/extend/plugins/the-events-calendar/other_notes/

    This may be a better option for you

    eventDisplay=upcoming

    <?php $custom=new WP_Query('cat=7&eventDisplay=upcoming'); ?>

    This automatically filters events so only the upcoming events show so you don't need to do the date comparison.

    I did find that to limit the number of events utilizing this method I had to utilize a post counter.

  3. webjedi
    Member
    Posted 3 years ago #

    @red321 -> The problem I am having and the reason why haydenhancock had to write that code above is that the 'eventDisplay=upcoming' argument doesn't work.

    It seems to grab a future event yes but not the next nearest future event.

    WJ

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic