After redirct url pagination not working - php

This is my pagination code. It's worked well after redirect page url. When I am redirecting url to another page this code not worked. This custom post pagination.
before redirect the pagination hover url was:
http://xxxx/?industry=26&posts_per_page=12&page=3
and after redirect the pagination hover url was (and its not working):
http://xxxx/newpage/?page=2&industry=26&location_company=0&employees=0&type=video
<?php
echo paginate_links( [
'prev_text' => __( 'Previous', 'bizcast' ),
'next_text' => __( 'Next', 'bizcast' ),
'before_page_number' => '',
'screen_reader_text' => '',
'total' => $videos->max_num_pages,
'format' => '?page=%#%',
'current' => ($_GET['page'] ? $_GET['page'] : 1),
'type' => 'list'
] );
?>

Add
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
before loop start and add 'paged' => $paged in wp query arguments.
For example:
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'category_name' => 'tutorials',
'posts_per_page' => 5,
'paged' => $paged
);

Related

WP_Query pagination on a custom endpoint in WooCommerce

I have created a class that creates a custom endpoint for WooCommerce My Account and on this endpoint I am trying to add the lists of products with pagination using WP_Query.
Here is the code:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new \WP_Query( $args );
while( $query->have_posts() ):
$query->the_post();
echo get_the_title().'<br>';
endwhile;
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
The pagination links appear just fine and the correct number of page links.
The problem is that the get_query_var('paged') is always 0.
I checked using var_dump get_query_var('paged'). I am guessing that it has something to do with the fact that I am creating the WP_Query instance on an endpoint.
Anyone have any ideas if WP_Query with pagination can work on a custom endpoint?
After getting some good sleep, I came back to this issue this morning and with fresh eyes and I could see what was causing the issue.
As this was a custom endpoint the 'paged' variable was being added to the custom endpoint query variable, in this case called 'listings'.
If I var_dump the listings query variable I get page/2 etc. So I wrote
if( empty( get_query_var('listings') ) ):
$paged = 1;
else:
$paged_array = explode('/',get_query_var('listings'));
$paged = $paged_array[1];
endif;
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'paged' => $paged
);
This allowed me to get a paged variable and now the pagination works.
Seems like a bit of a hack, but not sure how else to get a paged variable from an endpoint

Fix pagination after Wordpress 5.5 update

After updating my website to Wordpress 5.5 pagination broke.
If example.com/news/ is the news page for my website, then following example.com/news/2/ (that opened the second page of posts before the update) redirects me to example.com/news/.
It is a known problem of the 5.5 update, but I didn't manage to fix it by changing the variable's name.
Here is how I use pagination on my website:
The query part:
$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$query = new WP_Query( array(
'posts_per_page' => 5,
'paged' => $page
));
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
The pagination part:
echo paginate_links( array (
'base' => get_permalink( get_option( 'page_for_posts' ) ) . '%_%',
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'page' ) ),
'format' => '%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => '←',
'next_text' => '→',
'add_args' => false,
'add_fragment' => '',
));
wp_reset_postdata();
Using either of this:
remove_filter('template_redirect', 'redirect_canonical');
remove_action('template_redirect', 'redirect_canonical');
causes 404 error at example.com/news/2/.
Temporary fix without touching the core: disable the 404 handling for paged urls via filter pre_handle_404 in your theme's functions.php:
function pre_handle_404($preempt, $wp_query)
{
if (isset($wp_query->query['page']) && $wp_query->query['page']) {
return true;
}
return $preempt;
}
add_filter( 'pre_handle_404', 'pre_handle_404', 10, 2 );

pagination error on category page in wordpress

Pagination not working on category page, the query that i used on category page is below:
number of paginate is correct but while I am clicking on that number it redirect to home page where I am wrong please give me solution,
//code below
$cat_ID = get_query_var('cat');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$article = new WP_Query(array('post_type' => 'post','cat' => cat_ID,
'posts_per_page' => '2', 'paged' => $paged));
while ($article->have_posts()) : $article->the_post();
$post_id = get_the_ID();
// here is my pagination code
$big = 76;
$args = array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'total' => $article->max_num_pages,
'current' => $paged,
'prev_next' => True,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'type' => 'list');
echo paginate_links($args);
<?php while (have_posts()):the_post(); ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$slide = new WP_Query(array('post_type' => 'post', 'posts_per_page' => '12', 'paged' => $paged));
if ($slide->have_posts()) : while ($slide->have_posts()) : $slide->the_post();
$post_id = get_the_ID();
//show contents here
$big = 76;
$args = array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'total' => $slide->max_num_pages,
'current' => $paged,
'prev_next' => True,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'type' => 'list');
// ECHO THE PAGENATION
echo paginate_links($args);
hello friend i have faced this type of problem before and the solution that i came with was
Go to setting and then go to to reading tab
there you can see "Blog pages show at most" option
adjust the number according to your need or until the pagination starts working
Change 'format' parameter 'paged' to anything else.
echo paginate_links( array(
...
'format' => '?myparam=%#%',
Then access/get it from your url something like this
$page = (get_query_var('myparam')) ? get_query_var('myparam') : 1;
$slide = new WP_Query(array('post_type' => 'post', 'posts_per_page' => '12', 'paged' => $page));
$category_link = get_category_link( $category_id ) . '/%_%';
'base' => $category_link,
'format' => 'page/%#%',
'total' => $article->max_num_pages

Pagination links is not working in wordpress

Pagination links are not working in my blog post in woocommerce...
when i click 2 the second page http://www.freshcropmushrooms.com.au/blog/page/2 its redirect to the http://www.freshcropmushrooms.com.au/blog/
on the otherhand next link is also not working
I use the following code in pagination.php
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $wp_query;
if ( $wp_query->max_num_pages <= 1 )
return;
?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
'base' => str_replace( 999999999, '%#%', get_pagenum_link( 999999999 ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) ) );
?>
</nav>
Please fix it for me
pagination doesn't work correctly with queries unless they're on a page.
you might need to re-read that part. what query_post() does is destroy the standard query.
It would be like
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'category_name' => 'News',
'paged' => $paged //replace it with your aray key
);
query_posts($args);

Pagination link is not working /page/2 - NOT FOUND - Wordpress

I need to create a paginator in my blog page, until this its good, but when i click in a link of my pagination i got NOT FOUND page, i need to know if i need to able something in the panel to wordpress able the access to ?page=N
function:
function get_pagination($the_query) {
global $paged;
$total_pages = $the_query->max_num_pages;
$big = 999999999;
if ($total_pages > 1) {
ob_start();
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => $paged,
'total' => $total_pages,
'prev_text' => '',
'next_text' => ''
));
return ob_get_clean();
}
return null;
}
my blog code
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// echo $paged;
$produtos = new WP_Query(array(
'post_type' => 'blog',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'asc',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'categorias',
'field' => 'slug',
'terms' => ACTIVE
)
)
));
while ( $produtos->have_posts() ) : $produtos->the_post();
//CONTENT
endwhile;
echo get_pagination($produtos);
Go to admin Dashboard then Settings->Reading then set Blog pages show at most is equal to you query posts_per_page. So in your query if you set posts_per_page => 2 then Blog pages show at mostwill be 2
This is what I found and resolved the issue I had!
[...] I needed to go into the wp-admin page (the wordpress dashboard)
and go to Settings then Reading and in the "Blog pages show at most"
field I changed the value from '10' to '6' (the number of posts I
indicated in
$wp_query->query('showposts=6&cat=1'.'&paged='.$paged);)
use following paged query
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$produtos = new WP_Query(array(
'post_type' => 'blog',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'asc',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'categorias',
'field' => 'slug',
'terms' => ACTIVE
)
)
));
while ( $produtos->have_posts() ) : $produtos->the_post();
//CONTENT
endwhile;
echo get_pagination($produtos);
Please check your .htaccess file. It should contain a rewrite rule to enable pagination with slashes.
Please see:
"Using pretty permalinks" - http://codex.wordpress.org/Using_Permalinks
Problem: When we click on next page then wordpress redirects on first
------- page or on same pag.
Solution: put this code snippet in your themes functions.php file.
--------
add_filter('redirect_canonical', 'pif_disable_redirect_canonical');
function pif_disable_redirect_canonical($redirect_url)
{
if (is_singular()) $redirect_url = false;
return $redirect_url;
}
---------------------------------------------------
! it has worked for me , I hope it works for you
Go to your wordpress Dashboard Settings then Reading and in the "Blog pages show at most" field, changed the value from '10' to '1'
cheers!

Categories