This use to work, until wordpress updated and now the pagination on a custom page is repeating the first page. Page 2 for example will just show up page 1 content.
I've been looking and looking and nothing is helping.
I'm calling wordpress functions outside of wordpress install with
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
then the wordpress loop is (and it's probably outdated and faulty, but this was the only one that worked at the time)
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 20,
//'showposts' => 20,
'orderby'=> 'menu_order',
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
)
)
);
query_posts( $args); ?>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/producttemplate.php";
include_once($path);
?>
</div>
<br class="clear" />
<div class="wrapper">
<div class="pagination">
<p><br /><?php pagination_bar($args); if(!$_GET['viewall']){ ?>
<br />
<a class="all" href="<?php echo add_query_arg( array( 'view' => 'all' ), get_pagenum_link(1) ); ?>">Show All</a><br /></p>
<?php } }?>
<?php wp_reset_query(); ?>
and then my custom pagination function is
//Pagenation
function pagination_bar() {
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
echo paginate_links(array(
'base' => #add_query_arg('paged','%#%'),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $total_pages,
));
}
}
Thank you
The following are two lines copied out of your second code dump:
$current_page = max(1, get_query_var('paged'));
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
Notice the query_var is paged?
Now, have a look at this copy from the first line of the first dump:
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
Notice the difference? page, instead of paged.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'charters',
'cat'=> $cat,
'post_per_page'=> 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
wp_reset_query();
?>
<?php echo pnavigation( $query ); ?>
then you need to add following code to function.php :
function pnavigation( $wp_query ) {
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '←',
'next_text' => '→',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination pagination-lg">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
also add this code to function.php, you need to just replace url (client-testimonials) with your page url
function pagination_rewrite() {
add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
After pulling whats left of my hair out, I solved it. It was this part
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
Needs to be
require $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
I'm currently using both as a fallback. I don't know if it needs to just be one. If someone has more info if it's safe to use both?
Related
I am trying to add pagination for custom entries created through the CPT UI.
<?php $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; ?>
<?php
$query = new WP_Query( array(
'post_type' => array( 'feedbacks' ),
'posts_per_page' => 1,
'paged' => $paged,
));
?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post();?>
//post
<?php endwhile;?>
<?php endif; ?>
</div>
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query ->max_num_pages
));
?>
Pagination appears. But when you go to the second page gives 404.
We partially managed to solve the problem by adding the is archive attribute for the custom post type and changing the type of permalinks to default ones. In this case, everything works, but you need links with the page name
I have a loop with custom post types, and pagination doesn't appear, when I enter the URL with /page/2, /page/3... it shows the content correctly, but links don't appear on the page.
Here is the code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$parent_only_query = new WP_Query(array(
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
));
while ($parent_only_query->have_posts()){
$parent_only_query->the_post();
//content
}
pagination(); ?>
Archive page with pagination working:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //content ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?>
<?php endif; ?>
<?php pagination(); ?>
add this in your functions.php
function pagination_nav() {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) { ?>
<nav class="pagination" role="navigation">
<div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div>
</nav>
<?php }
}
display on page.php
<?php pagination_nav(); ?>
pagination can be shown in custom post type archive template and on custom template as well.
Pagination for archive template.
// current page
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array( 'post_type' => 'product',
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
new WP_Query( $args );
// Call pagination function before wp_reset_postdata()
the_posts_pagination( array(
'prev_text' => '<span class="fa fa-angle-left" aria-hidden="true"></span>',
'next_text' => '<span class="fa fa-angle-right" aria-hidden="true"></span>',
'screen_reader_text' => ' ',
'before_page_number' => '',
'mid_size' => 3,
) );
Pagination for custom Template
// Get current page.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array(
post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
$query = new WP_Query( $args );
$totalPage=$query->max_num_pages;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $totalPage
) );
You can check WordPress official document on Wordpress Codex
I read so many articles and didnt find answer.
my custom product loop in category:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
global $post;
$args = array(
'posts_per_page' => 6,
'product_cat' => $post->post_name, // **The father category**
'post_type' => 'product',
'paged' => $paged,
'page' => $page,
'pagination' => true
);
$loop = new WP_Query( $args );
echo $loop->request;
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
////////////////// product info//////////////
<?php endwhile;
$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' => $loop->max_num_pages
) );
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
in category page i have same link:
http://localhost:3000/product-category/cats/videocard/
pagination link:
http://localhost:3000/product-category/cats/videocard/page/2/
but i have 404 on it.
my permalink settings:
enter image description here
my "Read" settings:
enter image description here
What i do wrong?
Possibly your problem is related to the WP_Query arguments. I have spotted the following issues:
the 'pagination' is not a valid argument,
the taxonomy argument ( 'product_cat' ) given as key is deprecated, you should use 'tax_query' instead.
the $page variable value is not defined ( at least not in your sample script ) and it seems that the definition of both 'page' and 'paged' arguments might cause conflicts to your query.
Try using the following:
global $post;
$args = array(
'posts_per_page' => 6,
'post_type' => 'product',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $post->post_name // !
),
),
);
$loop = new WP_Query( $args );
// echo $loop->request; - I guess this is a test left-over
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
/**
* No need to call the $product global within the loop,
* use the $loop->post or even better call the product object
* using the WooCommerce wc_get_product() function as displayed
* below
*/
// global $product;
$product = wc_get_product( $loop->post );
?>
////////////////// product info//////////////
<?php endwhile;
$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' => $loop->max_num_pages
) );
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
Last, but not least, the taxonomy term value you are giving for product_cat ( commented as the father category ) is the current post slug. I understand why you might want to do this, but under some circumstances, this could be also the reason you are getting these 404s. Therefore, if the above script is not solving your problem, I would advise you to look into this too.
When I use pagination for custom post type product its working fine but its not working for the categories of custom post type. for ex. pagination working for this http://localhost/wordpress/products/page/2/ and not for this http://localhost/wordpress/products/landscape/page/2/ its always showing the first page content. How to solve this? given below is my code.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'taxonomy' =>'product_cat','term' => $cat_name1,'orderby'=>'post_date','page'=>$paged );
$wp_query = new WP_Query($args);
if($wp_query->have_posts()) : while ($wp_query->have_posts()): $wp_query->the_post();
<div class="product_list">
<?php the_title();?>
</div>
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $wp_query ) );//plugin code ?>
<?php else : ?>
<!-- No posts found -->
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo "No Products found for this categoy!." ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
Try this :
-Replace 'page' arguments with 'paged'
-Replace 'taxonomy' with 'tax_query'.
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$taxonomy_terms = get_terms( $taxonomy, array(
'hide_empty' => 0,
'fields' => 'ids'
) );
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $taxonomy_terms,
),
),'orderby'=>'post_date','paged'=>$paged );
In your question, you have used $cat_name1 for terms listing then please use following code:
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $cat_name1,
),
),'orderby'=>'post_date','paged'=>$paged );
Pagination :
please replace wp_pagenavi() function with following code:
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, $paged ),
'total' => $wp_query->max_num_pages
) );
can somebody tell me why this code give me url like : "http://localhost/page/1215752191/" and not url like "http://localhost/page/2/" ?
this is the code:
function mytheme_paginate() {
global $paged, $custom_query;
$big = 99999999999;
$args = array (
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var( 'paged' )),
'total' => $custom_query->max_num_pages,
'show_all' => False,
'end_size' => 2,
'mid_size' => 2,
'prev_next' => True,
'prev_text' =>__( '<' ),
'next_text' =>__( '>' ),
'type' => 'list'
);
echo paginate_links( $args );
}
In the index.php I have a custom loop and the code here looks like this:
<?php
$custom_query_args = array(
'posts_per_page' => 2,
'category_name' => 'news',
);
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
the_content();
endwhile;
endif;
// Reset postdata
wp_reset_postdata();
// Custom query loop pagination
mytheme_paginate();
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>