Reputation: 1110
Im trying to see the best way to implement this feature. I have events that are shown as long as they are within the date parameters as so:
<?php $args = array(
'post_type' => 'events',
'orderby' => '_cmb_event_date_timestamp',
'meta_key' => '_cmb_event_date_timestamp',
'order' => 'ASC',
'meta_query' => array(
array( /* Timestamp query to hide old dates */
'key' => '_cmb_event_date_timestamp',
'value' => strtotime('today'),
'compare' => '>'
)
),
);
$q = new WP_Query($args);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
I have two buttons: One saying upcoming events
and the other saying past events
.
I want to run the reverse query 'compare' => '<'
when past events
is clicked but I can't see how best to implement this feature. Any suggestions/ways to implement this would be appreciated. I tried reloading using jquery but couldn't get it to work.
Upvotes: 0
Views: 3677
Reputation: 10190
Two easy options:
1) Create a new page template for "Past Events" that uses that query instead, and simply have the "Past Events" button link to that page (this is by far the easiest to implement).
2) Use AJAX to send the request to the server, which can then run the query and return the data and/or the HTML which you then update on the page with jQuery (this is a bit more work but will not cause the page to reload).
Alex's answer is also a clever and simple solution to the problem.
Upvotes: 0
Reputation: 301
How do you get the content? Ajax? If not - just simply add specific $_GET parameter to link and catch it when you build the wp query. E.g.:
<?php $args = array(
'post_type' => 'events',
'orderby' => '_cmb_event_date_timestamp',
'meta_key' => '_cmb_event_date_timestamp',
'order' => 'ASC',
'meta_query' => array(
array( /* Timestamp query to hide old dates */
'key' => '_cmb_event_date_timestamp',
'value' => strtotime('today'),
'compare' => '>'
)
),
);
if (isset($_GET['past_events'])) {
$args['meta_query'][0]['compare'] = '<';
}
$q = new WP_Query($args);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
And the link:
<a href="<?php echo esc_attr( add_query_arg( 'past_events', '' ) ); ?>">past events</a>
Upvotes: 6