I can't get pagination to work in WP query - wordpress

This is what my current wordpress post query looks like:
<?php
$new_loop = new WP_Query( array(
'post_type' => 'news',
'posts_per_page' => 5
) );
?>
I want to add the following pagination to it:
<?php the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( 'Prev'),
'next_text' => __( 'Next'),
) ); ?>
I googled for various solutions. Everywhere it said to add "paged" to the array, like so:
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;**
$new_loop = new WP_Query( array(
'post_type' => 'news',
'paged' => $paged,**
'posts_per_page' => 5 // put number of posts that you'd like to display
) );
?>
However, this does not work. How can I get the pagination to work in a custom wordpress post query?

I think you're missing the argument: 'current' => max( 1, get_query_var( 'paged' ) )
After you run your loop, you can add this function in your theme(functions.php or elsewhere):
if ( ! function_exists( 'custom_pagination' ) ) {
function custom_pagination( $args = array(), $class = 'pagination' ) {
if ( $GLOBALS['wp_query']->max_num_pages <= 1 ) {
return;
}
$args = wp_parse_args(
$args,
array(
'mid_size' => 3,
'prev_next' => true,
'prev_text' => __( 'Previous', 'theme' ),
'next_text' => __( 'Next', 'theme' ),
'screen_reader_text' => __( 'Posts navigation', 'theme' ),
'type' => 'array',
'current' => max( 1, get_query_var( 'paged' ) ),
//'total' => $the_query->max_num_pages,
)
);
$links = paginate_links( $args );
?>
<nav aria-label="<?php echo $args['screen_reader_text']; ?>">
<ul class="pagination">
<?php
foreach ( $links as $key => $link ) {
?>
<li class="page-item <?php echo strpos( $link, 'current' ) ? 'active' : ''; ?>">
<?php echo str_replace( 'page-numbers', 'page-link', $link ); ?>
</li>
<?php
}
?>
</ul>
</nav>
<?php
}
}
Then finally call it in whatever template you need to:
custom_pagination();

Related

Wordpress pagination on front-page not working

I have problem with pagination on a static front-page I have coded. This is not working setting to homepage:
<?php if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}else {
$paged = 1;
}
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 36,
'category_name' => $cat_slug[3],
'paged' => $paged
));
while ($query->have_posts()) : $query->the_post(); ?>
how to fix them?
You can try as follows -
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}else {
$paged = 1;
}
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 36,
'category_name' => $cat_slug[3],
'paged' => $paged
));
while ($query->have_posts()) : $query->the_post();
// Print your post data
endwhile;
// Paginations
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' => '',
) );
wp_reset_postdata();
Add this code after your while loop
<div class="nav-links">
<div class="nav-previous"><?php echo get_next_posts_link( __( 'Older Entries', 'yourtextdomain' ), $query->max_num_pages ); ?></div>
<div class="nav-next"><?php echo get_previous_posts_link( __( 'Newer Entries', 'yourtextdomain' ) ); ?></div>
</div>
You can get Older Entries and Newer Entries via this.

Wordpress posts loop pagination - first page return 125 posts instead of 10 and the rest return 10

I'm trying to show 10 posts per page with pagination on WordPress and the first page return 125 posts instead of 10 and the rest of the pages return 10 posts as requested, please assist :)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$output = array();
global $post;
$args = array('nopaging' => false, 'paged' => $paged, 'posts_per_page' => 10, 'post_type' => 'post', 'order'=> 'DES', 'orderby' => 'date');
$postslist = new WP_Query( $args );
if ( $postslist->have_posts() ) :
while ( $postslist->have_posts() ) : $postslist->the_post();
array_push($output, array("timestamp" => get_the_date('U'),"img_url" => get_the_post_thumbnail_url(), "title" => get_the_title(), "text" => get_the_content()));
endwhile;
wp_reset_postdata();
endif;
Here is the sample code which you can use for the pagination:
<?php
/**
* Looping through the content
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query (array(
'post_type' => 'custom_portfolio',
'orderby' => 'post_id',
'posts_per_page' => 10,
'paged' => $paged,
'order' => 'DESC'
)); ?>
<?php while ($loop -> have_posts()) : $loop -> the_post(); ?>
<?php endwhile; ?><?php wp_reset_query(); ?><?php wp_reset_postdata(); ?>
And here is the part for the pagination:
<!-- Pagination -->
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $loop->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', __( 'Previous Page', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next Page', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
Here is a good start for you, just adopt as you want.
The sticky posts was the problem, I have excluded the sticky_posts from the query, and it fixed the problem
'ignore_sticky_posts' => 1

The pagination in wordpress not work correctly

I have a problem with pagination for the custom post. I used these code bellow for my site:
The content still show content on loop are correct but when I click next number, it still in first page without jumpping to the next page.
How to fix this issues ?
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'category_name' => 'nha-dat-binh-duong',
'posts_per_page' => 2,
'paged' => $paged
) );
?>
<?php if ( $query->have_posts() ) : ?>
<!-- begin loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
GET CONTENT HERE
<?php endwhile; ?>
<!-- end loop -->
<div class="pagination">
<?php
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', __( 'Tin mới nhất', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( '⏩', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>```
Have you refreshed your permalinks? Simply go to permalinks in WordPress and then refresh the page.

404 error on pagination for custom post type, custom taxonomies

Page numbers and buttons display on a custom post type archive and the archives for custom taxonomies but render 404.
What am I doing wrong?
Here's the query for the custom post type archive:
<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
'post_type' => 'tracker',
'publish_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
) );
}
if ( $exec_query->have_posts() ) { ?><?php global $wp_query; $posts_per_page = $wp_query->query["posts_per_page"]; $posts_found = $wp_query->found_posts; ?><?php while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
This is the pagination code that I am using for the custom post type archive:
<section class="pagination">
<?php global $exec_query;
$big = 999999999;
$translated = __( 'Page', 'theme-name' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
'format' => 'page/%#%/',
'current' => max( 1, get_query_var('paged') ),
'prev_text' => __('« Previous'),
'next_text' => __('Next »', $exec_query->max_num_pages),
'total' => $exec_query->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.'</span> '
) );
?>
</section>
Here's the query for one of the custom taxonomies:
<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $post_type = get_queried_object(); echo $post_type->rewrite['slug']; $loop = new WP_Query( array(
'post_type' => 'tracker',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'taxonomy1',
'field' => 'slug',
'terms' => $post_type,
),
),
) ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
This is the pagination code for one of the taxonomies:
<section class="pagination">
<?php global $loop;
$big = 999999999;
$translated = __( 'Page', 'theme-name' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
'format' => 'page/%#%/',
'current' => max( 1, get_query_var('paged') ),
'prev_text' => __('« Previous'),
'next_text' => __('Next »', $loop->max_num_pages),
'total' => $loop->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.'</span> '
) );
?>
</section>

How to add woocommerce-pagination and woocommerce-ordering dropdown to custom shortcode

I've created a custom shortcode to display products with a minimum stock amount and would like to add pagination to the results as well as calling the woocommerce-ordering dropdown to be displayed on the page.
Here's the shortcode:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
),
$atts, 'minimum_stock'
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
Any help would be very much appreciated!
Kind regards,
JP
Okay, so I've got pagination working and tidied things up a bit (it was throwing some errors in the debug log), the code now looks like this:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $product, $woocommerce, $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
),
$atts, 'minimum_stock'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
if($products->max_num_pages>1){
?>
<nav class="woocommerce-pagination">
<?php echo paginate_links( apply_filters(
'woocommerce_pagination_args', array(
'base' => esc_url( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $products->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
)
)
);
?>
</nav>
<?php }
woocommerce_reset_loop();
wp_reset_postdata();
$return = '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
Does anyone know how I can now call the woocommerce-ordering dropdown?
I've tried adding:
<?php do_action( 'woocommerce_before_shop_loop' ); ?>
But this doesn't seem to work, when I check the page it does have the 'woocommerce-notices-wrapper' but there's no sign of the 'woocommerce_catalog_ordering' that I thought should also be called with 'woocommerce_before_shop_loop'.
Any and all help will be very much appreciated :)
Update:
I found this question Adding 'sort by' drop down on custom page using woocommerce short code which gave me the answer I needed to add the woocommerce-ordering dropdown.
Here's my updated code:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'date',
'order' => 'desc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
), $atts );
if ( ! $atts['category'] ) {
return '';
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Default ordering args
$ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'],
$atts['order'] );
$orderby = 'date';
$order = 'desc';
if ( isset( $_GET['orderby'] ) ) {
$getorderby = $_GET['orderby'];
}
if ($getorderby == 'date') {
$orderby = 'date';
$order = 'desc';
} elseif ($getorderby == 'sku_desc') {
$orderby = 'meta_value';
$order = 'desc';
$meta_key = '_sku';
} elseif ($getorderby == 'sku_asc') {
$orderby = 'meta_value';
$order = 'asc';
$meta_key = '_sku';
}
$args = array(
'post_type' => array( 'product', 'product_variation' ),
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $orderby, // $ordering_args['orderby'],
'order' => $order, // $ordering_args['order'],
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<div style="width:100%;">
<div style="float:right">
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php
$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
'date' => __( 'Sort by latest', 'woocommerce' ),
'sku_asc' => __( 'A-Z / Low to High Numbers', 'woocommerce' ),
'sku_desc' => __( 'Z-A / High to Low Numbers', 'woocommerce' )
) );
foreach ( $catalog_orderby as $id => $name )
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $getorderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
?>
</select>
<?php
// Keep query string vars intact
foreach ( $_GET as $key => $val ) {
if ( 'orderby' === $key || 'submit' === $key )
continue;
if ( is_array( $val ) ) {
foreach( $val as $innerVal ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
}
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
?>
</form>
</div>
</div>
<div style="clear:both;"></div>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
if($products->max_num_pages>1){
?>
<nav class="woocommerce-pagination">
<?php echo paginate_links( apply_filters(
'woocommerce_pagination_args', array(
'base' => esc_url( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $products->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
)
)
);
?>
</nav>
<?php }
woocommerce_reset_loop();
wp_reset_postdata();
$return = '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
I hope someone might find this helpful :)
Kind regards,
JP

Resources