John Davidson

php - Pulling Next Event with Events Calendar & Ajax Load More

0 comments
Message:


I am editing a site that is using Ajax Load More to pull in info from the Events Calendar plugin. The client wishes for this area to pull the next post, instead of whatever past event it is pulling right now and I could use some help.


This is the site: https://milfordpa.us/ and the section is the one for schoolhouse days.


This is the content within the Repeater Template of the ALM plugin:


// Ensure the global $post variable is in scope
global $post;

// Retrieve the next upcoming events
$events = tribe_get_events( [ 'posts_per_page' => 1 ] );
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $event->ID ), 'full' );

// Loop through the events: set up each one as
// the current post then use template tags to
// display the title and content
foreach ( $events as $post ) {
setup_postdata( $post );
?>

<?php
if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
<?php endif;
}

?>
<div class="featured-event" style="background-image: url('<?php echo $image[0]; ?>')">
<div class="row">
<div class="content">
<?php
// This time, let's throw in an event-specific
// template tag to show the date after the title!
echo '<h2>' . $post->post_title . '</h2>';
echo '<p> ' . tribe_get_start_date( $post ) . '</p> ';
?>
<div class="fusion-button-wrapper">
<style type="text/css">
.fusion-button.button-3 {
border-radius: 0px;
}
</style><a class="fusion-button button-flat fusion-button-default-size button-default fusion-button-default-span fusion-button-default-type" target="_self" href="<?php echo tribe_get_event_link() ?>"><span class="fusion-button-text">Learn More</span></a>
</div>
</div>
</div>
</div>


My Answer:
To pull the next event from the Events Calendar in PHP, you can use the following code snippet:

php
// Get the current date
$current_date = date('Y-m-d H:i:s');

// Query to get the next event
$args = array(
'post_type' => 'event', // Change 'event' to the actual post type of your events
'posts_per_page' => 1,
'meta_key' => 'event_date', // Change 'event_date' to the meta key where the event date is stored
'meta_value' => $current_date,
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
);

$events_query = new WP_Query($args);

if ($events_query->have_posts()) {
while ($events_query->have_posts()) {
$events_query->the_post();
// Display the event details
the_title();
the_content();
// You can display other event details here
}
} else {
echo 'No upcoming events';
}

wp_reset_postdata();


Make sure to replace 'event' with the actual post type of your events and 'event_date' with the meta key where the event date is stored in your Events Calendar plugin. This code will query the database for the next event based on the current date and display its details.

Rate this post

3 of 5 based on 9098 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us