There are some things that should be changed in your code:
- Please no not use
$_SESSION
stuff. So do not add session_start anywhere. You can pass the current state via the query string, usingmethod="get"
in your form. - For a better flow, you can pass the current page to
search_v1
function as argument, in this way you are sure will be not errors - When you have to access superglobals array, instead of accessing directly is recomned to use
filter_input
- in the
overview-search-input.php
file, last line is$query = search_v1($_SESSION['period']);
that is completely useless - I think is useful in the form select the period option of the current showed posts, e.g. when you are viewing posts for period ‘upcoming’, in the form should be selected the option ‘upcoming’.
Applying this changes:
page.php
andsingle.php
and stay unchanged.- remove
session_start();
from where you have added
Then
overview-search-input.php
<?php
$period = filter_input(INPUT_GET, 'period', FILTER_SANITIZE_STRING);
$paged = get_query_var('paged');
if ( empty($period ) ) $period = 'upcoming';
if ( empty($paged) ) $paged = 1;
overview-sidebar.php
<form action="<?php echo get_permalink(); ?>" method="get">
<select name="period" placeholder="Select">
<option <?php selected('', $period) ?>></option>
<option <?php selected('all', $period) ?>>all</option>
<option <?php selected('upcoming', $period) ?>>upcoming</option>
</select>
</form>
overview-main.php
include_once 'overview-search-input.php';
include_once 'overview-sidebar.php';
$query = search_v1( $period, $paged );
if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
// Do stuff
endwhile;
echo create_pagination( $query, $period );
wp_reset_postdata();
endif;
Finally the 2 functions, search_v1
and create_pagination
:
function search_v1( $period = 'upcoming', $paged = 1 ) {
if ( empty($paged) ) $paged = 1;
$meta_query = array(
// create your meta query here
);
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array($meta_query),
'meta_key' => 'date_from',
'orderby' => 'meta_value',
'order' => 'ASC',
);
return new WP_Query( $args );
}
function create_pagination( $query, $period = 'upcoming' ) {
$current = isset($query->query_vars['paged']) ? $query->query_vars['paged'] : 1;
$args = array(
'base' => get_permalink() . '%_%',
'format' => '?paged=%#%&period=' . $period,
'total' => $query->max_num_pages,
'current' => ($current > 0) ? $current : 1,
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => '<i class="icon-chevron-left"></i>',
'next_text' => '<i class="icon-chevron-right"></i>',
'type' => 'list'
);
return paginate_links($args);
}