Support

Home Forums Event Espresso Premium Custom past event list showing trashed events

Custom past event list showing trashed events

Posted: May 24, 2024 at 4:09 am

Viewing 3 reply threads


PropertyBar

May 24, 2024 at 4:09 am

Hi there,

We are using this code for a custom past events page:

$args = array(
'limit' => 100,
'show_expired' => TRUE,
'sort' => 'DESC',
'order_by' => 'start_date',
);
add_filter( 'posts_where', 'custom_posts_where_sql_for_only_expired' );

Please could you let me know the update we can apply to stop it from showing deleted events? We have tried: ‘show_deleted’ => FALSE, ‘status’ => ‘publish’, ‘post_status’ => ‘publish’ but sadly none of these have worked.

Many thanks for your help,
PBA


Tony

  • Support Staff

May 24, 2024 at 4:38 am

Hi there,

You haven’t included all of the code above, it looks like you are doing this with a standard WP_Query class, correct?

What is custom_posts_where_sql_for_only_expired doing?

The reason I ask is the models make this easier in the long run and apply default where conditions so that things like trashed events are not included by default.

So using the model system (Docs HERE) something like this should match the above:

$where['Datetime.DTT_EVT_end'] = [
	'<=',
    EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
];
$query_params = [
	$where,
	'order_by' => 'Datetime.DTT_EVT_start', 
	'order' => 'DESC',
	'limit' => 100
];
$expired_events = EEM_Event::instance()->get_all($query_params);


PropertyBar

May 24, 2024 at 5:06 am

Hello,

Thanks so much for your quick reply, this is the full code in case that helps as hopefully we can just make a quick tweak to this to make it work?

	<?php

	// [ESPRESSO_EVENTS sort=DESC show_expired=true]

	$args = array(
		'limit'        => 100,
		'show_expired' => TRUE,
		'show_deleted' => FALSE,
		'sort'         => 'DESC', 
		'order_by'     => 'start_date',
	);
	add_filter( 'posts_where', 'custom_posts_where_sql_for_only_expired' );
	
	$loop = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $args );
	
	if( $loop->have_posts() ) {

		echo '<div class="events_list">';
	
		// loop through posts
		while ( $loop->have_posts() ) : $loop->the_post();

        $datetime = espresso_event_date_obj($post->ID);
        // $start_day = $datetime->start_date('l');
        // $start_time = $datetime->start_time('g:i a');
        // $start_date = $datetime->start_date('M j, Y');
        $start_date = $datetime->start_date('d M Y');
        // $end_time = $datetime->end_time('g:i a');
        // $end_date = $datetime->end_date('F j, Y');
        $end_date = $datetime->end_date('d M Y');

        $output_date = $start_date;
        if ($start_date != $end_date) {
            $output_date .= ' - '.$end_date;
        }

        $URL = add_query_arg(['ee' => 'download_ics_file', 'ics_id' => $datetime->ID()], site_url());
        $URL = esc_url_Raw($URL);

        $event_location = get_field('event_location');
        $event_location_link = get_field('event_location_link');
        if ($event_location_link) {
            $event_location = '<a href="'.$event_location_link.'">'.$event_location.'</a>';
        }

        echo '<article>';
        echo '<p>'.$output_date.'</p>';
        echo '<h2><a href="'.get_the_permalink().'">'.get_the_title().'</a></h2>';
        echo '<p><img src="/wp-content/themes/propertybar/images/icon-pin.svg" width="6" height="12" alt="Map pin">'.$event_location.' <a href="'.$URL.'" class="pba_add_calendar">Add to calendar</a></p>';
        echo '<p><a href="'.get_the_permalink().'">View event</a></p>';
        echo '</article>';

        // espresso_event_date()

		endwhile; 

		echo '</div>';

		}
	remove_filter( 'posts_where', 'custom_posts_where_sql_for_only_expired' );
	wp_reset_postdata();
	?>

	<?php // pagination links
	$big = 999999999; // need an unlikely integer
	$pagination = paginate_links( array(
		'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
		'format' => '?paged=%#%',
		'current' => max( 1, get_query_var('paged') ),
		'total' => $loop->max_num_pages,
		'show_all'     => TRUE,
		'end_size'     => 10,
		'mid_size'     => 6,
		'prev_next'    => TRUE,
		'prev_text'    => '&lsaquo; PREV',
		'next_text'    => 'NEXT &rsaquo;',
		'type'         => 'plain',
		'add_args'     => FALSE,
		'add_fragment' => ''
	));
	echo ! empty( $pagination ) ? '<div class="ee-pagination-dv clear">' . $pagination . '</div>' : '';
	?>

Many thanks again,
PBA


Tony

  • Support Staff

May 24, 2024 at 3:16 pm

Oh, your using and EventListQuery, which shouldn’t include trashed events by default.

Just to confirm, when you say its includes deleted events, are those trashed events or datetimes?

Using your code above doesn’t include trashed events for me so just trying to narrow this down some more.

What is the custom_posts_where_sql_for_only_expired function doing?

Viewing 3 reply threads

The support post ‘Custom past event list showing trashed events’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso