wordpress pagination ads extra slashes to url - wordpress

Working with custom post types I cant seem to get the pagination to work correct. All is working but when I click to the next page the url will get a extra slash in the url, not sure why this is happening but when using the pagination a lot will add every time a new slash to the url.
// out
site.com/projects/ ,
site.com/projects//page/2,
site.com/projects///page/3,
site.com/projects////page/4
...and so on.
// the code used
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( array(
'post_type' => 'projects',
'posts_per_page' => 5,
'paged' => $paged
) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
the_title();
endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => esc_html__('volgende »', 'infrafocus'),
'next_text' => esc_html__('« vorige' , 'infrafocus'),
));
}
}
wp_reset_postdata();
endif;

Sometimes in the case of Custom post type pagination, you have to add the rewrite rule in your function.php
Add Function like:
function custom_rewrite_basic() {
add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php/projects?page=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Please Refer this link for more information : https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

Try changing 'format' => '/page/%#%', to 'format' => 'page/%#%',

User format as below
'format' => '?paged=%#%'
https://codex.wordpress.org/Function_Reference/paginate_links

Related

WP_Query - posts_per_page parameter not working?

So the setup is working fine, however I tried to limit the number of titles shown but nothing is working. What am I missing? So it should be limited to 10 or 5 titles.
I tried everything I could think of but for some reason it is not taking the limit in account.
<?php
$meta_query = array();
$args_booking = array(
'post_type' => 'booking',
'post_status' => array('publish', 'pending', 'canceled'),
'nopaging' => 'false',
'posts_per_page' => 1,
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
),
);
$meta_query[] = array(
'key' => GOLO_METABOX_PREFIX. 'booking_item_author',
'value' => $user_id,
'type' => 'NUMERIC',
'compare' => '=',
);
$args_booking['meta_query'] = array(
'relation' => 'AND',
$meta_query
);
//$data_booking = new WP_Query($args_booking);
//$total_post = $data_booking->found_posts;
if( count($results) > 0 ){//if( $total_post > 0 ){
?>
<ul class="listing-detail custom-scrollbar">
<?php foreach ($results as $r):?>
<?php
$lang = $r->lang!='nl'?'/' . $r->lang : '';
$param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);
$url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
?>
<li><?php echo get_city($r->city_id)->name;?></li>
<?php endforeach;?>
</ul>
<?php
}else{
?>
<span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
<?php
}
?>
There are a couple of filters that run before WP_Query actually executes the desired SQL command, which means you posts_per_page setting can actually get overridden before your results are returned.
Unfortunately this can be done in both the theme you may be using, and in any plugins that maybe active. What I would do in this situation, is first check any of my other code to see if I am modifying the pre_get_posts or the post_limits filter.
I'd then check the theme settings to see if it has a setting for this, and last resort would be to disable plugins one by one to see if any of those are filtering the query.
Lastly don't discount cache being an issue. Some hosts can force caching of pages, even when you are logged in to WordPress.
I corrected this like this.
So you can take a pattern.
Let me know if it does not work or does
I used posts_per_page to set the number of posts per page (number of titles per page) Or you can use numberposts.
numberposts is used to search and display only 20 posts and posts_per_page is used to display 20 posts per page.
$args_booking = array(
'post_type' => 'booking',
'post_status' => array('publish', 'pending', 'canceled'),
'nopaging' => 'false',
'posts_per_page' => 20, // for set limit.
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
),
'meta_query' => array(
array(
'key' => GOLO_METABOX_PREFIX . 'booking_item_author',
'value' => $user_id,
'type' => 'NUMERIC',
'compare' => '=',
)
)
);
$query = new WP_Query($args_booking);
$results = $query->get_posts();
if (count($results) > 0) {//if( $total_post > 0 ){
?>
<ul class="listing-detail custom-scrollbar">
<?php foreach ($results as $r): ?>
<?php
$lang = $r->lang != 'nl' ? '/' . $r->lang : '';
$param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);
$url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
?>
<li>
<a href="<?php echo $url; ?>" class="place-view"
data-id="<?php echo $r->ID; ?>">
<?php echo get_city($r->city_id)->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php
} else {
?>
<span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
<?php
}

Pagination not working for ACF Query on front-page.php

I'm using the following query based on the ACF documentation. For some reason, the pagination isn't working. Can anyone tell me what I'm missing? The pagination code is working fine on an archive.php page.
front-page.php
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'posts_per_page' => 6,
'post_type' => 'airdrop',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'airdrop_type',
'value' => array('Airdrop'),
'compare' => 'IN',
)
)
);
$the_query = new WP_Query( $args ); ?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part('airdrops/full-width'); ?>
<?php endwhile; ?>
<div class="col-md-12">
<?php html5wp_pagination(); ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
pagination code in functions.php
add_action('init', 'html5wp_pagination');
function html5wp_pagination()
{
global $wp_query;
$big = 999999999;
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
));
}
check this
Pagination_Parameters
And http://prntscr.com/kt47rs
You don't need use add_action('init','...') function for pagination.
and use calling function like : html5wp_pagination($the_query );
Please check, replace with this
function html5wp_pagination()
{
global $the_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $the_query->max_num_pages
));
}
Using a plugin may not be the best solution but I ended up using WP-PageNavi which allows wp_query. This worked for me and solved the problem.
Documentation can be found here: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html

Multiple post query with pagination in shortcode

I want to multiple query with pagination of post type POST. I write code but it don't return right post when click for 2nd page or 3rd page. May be pagination is working properly or other errors. I want to multiple query with pagination of post type POST. I write code but it don't return right post when click for 2nd page or 3rd page. May be pagination is working properly or other errors. I can't find it.
function blogpost_shortcode($atts, $content = null){
extract( shortcode_atts( array(
'count' => '1',
), $atts) );
$page = get_query_var('page');
$q1 = new WP_Query( array(
'posts_per_page' => $count,
'post_type' => 'post',
'paged' => get_query_var('page')
) );
$q1_ids = wp_list_pluck( $q1->posts, 'ID' );
$q2 = new WP_Query(array('posts_per_page' => $count, 'post_type' => 'post', 'paged' => get_query_var('page'), 'post__not_in' => $q1_ids));
$blog_markup = '';
$blog_markup .= '
<div class="bwog_blog_wrap">';
while($q1->have_posts()) : $q1->the_post();
$idd = get_the_ID();
$blog_markup .= '
<div class="single_blog_wrap">
<h3>'.get_the_title($idd).'</h3>
</div>
';
endwhile;
while($q2->have_posts()) : $q2->the_post();
$idd = get_the_ID();
$blog_markup .= '
<div class="single_blog_wrap">
<h3>'.get_the_title($idd).'</h3>
</div>
';
endwhile;
$total_pages = $q1->max_num_pages;
$big = 999999999;
if ($total_pages > 1){
$current_page = max(1, get_query_var('page'));
$blog_markup.= '<nav class="page-nav">';
$blog_markup.= paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => 'Prev',
'next_text' => 'Next'
));
$blog_markup.= '</nav>';
}
$blog_markup .= '</div>';
wp_reset_query();
return $blog_markup;
}
add_shortcode('bwog_blog', 'blogpost_shortcode');
It looks like you are checking for the query variable page, but in the pagination code you are setting the format to ?paged=%#%. You'll want to make sure you are using the same query variable everywhere, so you should set the format to ?page=%#%.
Also, unrelated to the answer but something I noticed, you set the variable $page to get_query_var('page'), but then never use it. I would recommend using it everywhere you have get_query_var('page') to make it your code more efficient and maintainable.

WordPress pagination showing extra blank page after filtering past events

I have a list of events that are displaying in date order, with events in the past being hidden.
The issue is that in total, there are 3 pages of events, but once the past events are hidden, there are only enough events to fill 2 pages. However, a third, blank page is still present and can be accessed through the paging links.
This is the code on my events list page:
<?php $today = date("Ymd");?>
<?php $paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=upcomingevents&paged=' . $paged );?>
<?php $loop = new WP_Query( array( 'post_type' => 'upcomingevents', 'paged'=> $paged, 'meta_key' => 'start_date', 'meta_compare' => '>=', 'meta_value' => $today, 'orderby' => 'meta_value_num', 'order' => 'ASC' )); ?>
<?php while ( $loop->have_posts('post_type=upcomingevents') ) : $loop->the_post(); ?>
And this is the paging code in my functions file:
function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => #add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'plain'
);
if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
}
I have also discovered a second issue, when moving to page 2 or 3 of results, the CSS class of current remains on the number 1 within the paging, so impossible to tell which page you are on.
Any suggestions would be greatly appreciated, many thanks.
$paged value is 0 on 1st page so edit your first snippet:
$paged = 0;
also, as you're using WP_Query, you can safely delete following line, which is probably doing more wrong than good:
query_posts( '&post_type=upcomingevents&paged=' . $paged );

wordpress pagination not working in localhost server

I have used wordpress below code for pagination in local-host template but dont know why it is not working. Its showing all pages number nicely but any page number i click its only display first page.
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
));
}
my permalink settings is:
localhost/my-blog/sample-post/
and query post:
$args = array( 'post_type' => 'post', 'posts_per_page' => 2);
anybody please give me idea what to do next for working this pagination.
This works with WP_Query. You may try it:
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,
'end_size'=> 1,
'mid_size'=> 10 ) );
In Settings -> Readings set Blog pages show at most to the number of posts you want to display in each page and in Settings -> Discussion set the pagination behavior
<global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 ) {
// get the current page
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
// structure of "format" depends on whether we're using pretty permalinks
$format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
Please set a argument as per requirement and also refer this link My helps you
And let me know if you face problem
Thanks and Regards
I solved the same problem by reading the URL param using straight-up PHP, instead of get_query_var('paged'), simply use this:
$_GET['paged']

Resources