Display pages with the same message from page 1 on all other pages

Recently, they helped me a lot to create a list of upcoming events (see. Displaying upcoming events (including today's event) here? ), As a result, my pagination using WP Pagenavi has been violated.

At the moment, when you click on page 2, it just shows the same entries as the first page. Although the URL does change to page / 2 pages / 3, etc.

I have this in my functions.php file:

function filter_where( $where = '' ) { $where .= " AND post_date >= '" . date("Ymd") . "'"; return $where; } add_filter( 'posts_where', 'filter_where' ); $query = new WP_Query( array( 'post__not_in' => array(4269), 'paged' => get_query_var('paged'), 'post_type' => 'whatson', 'exclude' => '4269', 'post_status' => 'future,publish', 'posts_per_page' => 20, 'order' => 'ASC' ) ); remove_filter( 'posts_where', 'filter_where' ); 

My loop is as follows:

 <?php while ( $query->have_posts() ) : $query->the_post(); ?> // content <?php endwhile; // end of the loop. ?> <?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $query ) ); } ?> 
+4
source share
3 answers

Finally decided:

 function my_filter_where( $where = '' ) { global $wp_query; if (is_array($wp_query->query_vars['post_status'])) { if (in_array('future',$wp_query->query_vars['post_status'])) { // posts today into the future $where .= " AND post_date > '" . date('Ym-d', strtotime('now')) . "'"; } } return $where; } add_filter( 'posts_where', 'my_filter_where' ); 

and

 <?php $wp_query = array( 'post__not_in' => array(4269), 'paged' => get_query_var('paged'), 'post_type' => 'whatson', 'exclude' => '4269', 'posts_per_page' => 20, 'order' => 'ASC', 'orderby' => 'date', 'post_status' =>array('future','published')); query_posts($wp_query); ?> <?php if ($wp_query->have_posts()) { while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> Content <?php endwhile; // end of the loop. } ?> <?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); } ?> 
+4
source

Do you want it for a specific post or for all of them? If you need a general pagination, you can link to pages without plugins using this code snippet:

 <?php global $wp_query; $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query->max_num_pages ) ); ?> 

Just add it to index.php or archives.php and see the magic happen :)

+1
source

I'm not sure what happens with the rest of your code, but one attempt to try this will be a simple test: use wp_reset_query() in front of your new WP_Query to make sure the query variables have not been changed.

+1
source

Source: https://habr.com/ru/post/1488536/


All Articles