You might want to add Events to your WordPress site, but, as you might have noticed, WordPress does not display posts in the future, which are marked as “Scheduled” in the admin but “future” in the database (under post_status instead of “publish”).
The setup
Create a new Category if you have not already in Posts->Categories. Let’s call it “events”
Add your posts to the “event” category, then retrieve the category ID from clicking on Posts->Categories on the left hand admin menu. When in Categories click on the category “events” and in the URL you will find the ID – make a note of it.
https://morenafiore.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=18&post_type=post
There’s an easy workaround
In your Events template you will have to run the normal query to display the title and text you might have inserted in your Events page
(this is my version)
<?php if ( have_posts() ) : ?> <header class="entry-header"> <h1 class="entry-title"><?php the_title(); ?></h1> </header><!-- .entry-header --> <div class="entry-content"> <?php the_content(); ?> </div><!-- .entry-content --> <?php endif; ?>
Then start 2 queries, one for the future events – place the category ID you made a note of instead of the number 18 below:
$future_events = new WP_Query("cat=18&post_status=future&order=DESC"); if ($future_events->have_posts()){ while ($future_events->have_posts()) :$future_events->the_post(); ?> <article id="post-<?php echo $post->ID; ?>" class="event_post"> ... </article> <?php endwhile; } ?>
And then a second query to show the old postsĀ – place the category ID you made a note of instead of the number 18 below
$old_events = new WP_Query("cat=18post_status=publish&order=DESC"); if ($old_events->have_posts()){ while ($old_events->have_posts()) :$old_events->the_post(); ?> <article id="post-<?php echo $post->ID; ?>" class="event_post"> ... </article> <?php endwhile; } ?>
Bear in mind that unfortunately when using get_permalink() the link, even if correct will redirect to a 404, unless you are logged in. So to get round this problem, just add this to your functions.php and even posts in the future will be exposed to users.
add_filter('the_posts', 'show_future_posts'); function show_future_posts($posts) { global $wp_query, $wpdb; if(is_single() && $wp_query->post_count == 0) { $posts = $wpdb->get_results($wp_query->request); } return $posts; }
Sorted!
Recent Comments