WP pagination not working - wordpress

Im trying to add pagination to a page, but I can't even limit the number of posts shown. This is a strange issue, in the Settings -> reading I have posts per page set to 2, with no effect. Here is my query and loop:
<?php
$args = array(
'post_type' => array( 'webinar' ),
'post_status' => array( 'publish' ),
'posts_per_page' => '2',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'webinar-status',
'field' => 'id',
'terms' => 178
)
)
);
// The Query
$archived_webinar_query = new WP_Query( $args ); $counter = 1;
// The Loop
if ( $archived_webinar_query->have_posts() ) {
while ( $archived_webinar_query->have_posts() ) {
$archived_webinar_query->the_post(); ?>
<div class="classes<?php if ($counter % 2 == 0){ echo('f-right l-nmr'); } ?>">
<a href="<?php the_permalink(); ?>" target="_self" class="c-ltBlue"><?php the_title(); ?>
//other stuff here
</div>
<?php $counter++ ;}
// Restore original Post Data
wp_reset_postdata();
} else { ?>
<p class="l-twelve l-mb1 f-reg c-gray f-size16 f-l-height24">No archived webinars are available at this time.</p>
<?php }
?>

As per my comment, try 'posts_per_page' => 2, it's an integer type.
Your code should be
<?php
$args = array(
'post_type' => array( 'webinar' ),
'post_status' => array( 'publish' ),
'posts_per_page' => 2,
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'webinar-status',
'field' => 'id',
'terms' => 178
)
)
);
// The Query
$archived_webinar_query = new WP_Query( $args ); $counter = 1;
// The Loop
if ( $archived_webinar_query->have_posts() ) {
while ( $archived_webinar_query->have_posts() ) {
$archived_webinar_query->the_post(); ?>
<div class="classes<?php if ($counter % 2 == 0){ echo('f-right l-nmr'); } ?>">
<a href="<?php the_permalink(); ?>" target="_self" class="c-ltBlue"><?php the_title(); ?>
//other stuff here
</div>
<?php $counter++ ;}
// Restore original Post Data
wp_reset_postdata();
} else { ?>
<p class="l-twelve l-mb1 f-reg c-gray f-size16 f-l-height24">No archived webinars are available at this time.</p>
<?php }
?>
Edit: Improvements
You do not need a counter variable. WP_Query object provides a property for this. In your case, $archived_webinar_query->current_post will give you index for the current post in loop.
Reference to documentation

I use this function to add numeric pagination, and it's working with me.
I posted it to you maybe help you :)
Add this function to your functions.php
function theme_pagination($pages = '', $range = 3)
{
global $wp_query;
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$showitems = ($range * 2)+1;
if(empty($paged)) $paged = 1;
if($pages == '' && $pages != 0)
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
add this to the top of the loop global $wp_query;
and use theme_pagination($wp_query->max_num_pages); after wp_reset_postdata();

Related

Custom Post Type Pagination sends me to home page

For years I use a custom pagination script that I include to all my websites and it paginates flawlessly through posts. For the first time ever I created custom post types. My same script will not paginate through pages... Also all pages links send me to home. It displays exactly the correct pages number based on posts per page. It just won't work. I have used all scripts I could find on net.
Is there anything I am missing?
Has archive is true by the way. Below code example is from archive-drivers.php
I also used both custom CPT creation with script in functions and also used a plugin. Just to check if something was wrong.
<?php
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'Drivers',
'post_status' => 'publish',
'suppress_filters' => true
);
// get the results
$wp_query = new WP_Query( $args );
?>
<?php if ($wp_query->have_posts()): // the Loop ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
(...my code to display the custom post type posts...)
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- end of Code for the Loop -->
<?php endif; ?>
<!-- Code for PAGINATION -->
<?php
if (function_exists("custom_pagination"))
{
custom_pagination();
}
?>
<!-- End of Code for PAGINATION -->
<?php wp_reset_postdata(); ?>
<!-- end of Code for the Loop -->
and
// Bootstrap Custom Pagination function in Functions.php
function custom_pagination($pages = '', $range = 4)
{
$showitems = ($range * 2) + 1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo '<div class="as-pagination" style="text-align:center; align:center;">';
echo '<div> Page ' .$paged . ' of ' .$pages.'</div>';
echo '<ul>';
// If we want to always see th First Page link
if($paged > 1) echo '<li></i></li>';
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li class=\"active\"><span>".$i."</span>
</li>":"<li><a href='".get_pagenum_link($i)."'>".$i."</a></li>";
}
}
//if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<li>»</li>';
if($paged < $pages) echo '<li><i class="far fa-arrow-right"></i></li>';
echo "</ul>";
echo "</div>";
}
}
And the answer came from a FB friend and has to do with pre_get_posts. Only that way you can tell wp how many posts per page you want for your custom post type. The 'posts_per_page' in args won't work.
function my_cptui_change_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'drivers' ) ) {
$query->set( 'posts_per_page', 1 );
}
}
add_filter( ' ', 'my_cptui_change_posts_per_page' );

Wordpress pagination functions are not working (dispalying nothing)

I’m going nuts trying to figure out why my pagination isn’t showing up. I’ll set the scene:
I have a page "category.php". In this page I have a custom query set up for all of my categorize posts. They are custom post types and showing custom post of that category. My query is as follows:
I am already tried this function:
the_post_pagination();
global $wp_query;
$category = get_category(get_query_var('cat'));
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$cat = new WP_Query(
array(
'post_type' => 'shield',
'category_name' => $category->name,
'posts_per_page' => 10,
'paged' => $paged
));
if ( $cat->have_posts() ) {
while ( $cat->have_posts() ) {
$cat->the_post();
}
mom_pagination($cat->max_num_pages);
wp_reset_query();
}
mom_pagination function code goes here:
function mom_pagination($pages = '', $range = 10)
{
global $wp_query;
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
if (mom_option('pagi_type') == false) {
$showitems = ($range * 2)+1;
if(empty($paged)) $paged = 1;
if($pages == '' && $pages != 0)
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
} else {
$big = 999999999; // need an unlikely integer
echo "<div class='pagination'>";
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $pages
) );
echo "</div>\n";
}
}
i expect that it should display the pagination.
After an exhausted effort of a whole day finally find the solution.
i write these two functions in my functions.php file and finally i got the expected result.
function remove_page_from_query_string($query_string)
{
if ($query_string['name'] == 'page' && isset($query_string['page'])) {
unset($query_string['name']);
// 'page' in the query_string looks like '/2', so i'm spliting it out
list($delim, $page_index) = split('/', $query_string['page']);
$query_string['paged'] = $page_index;
}
return $query_string;
}
// I will kill you if you remove this. I died two days for this line
add_filter('request', 'remove_page_from_query_string');
// following are code adapted from Custom Post Type Category Pagination Fix by jdantzer
function fix_category_pagination($qs){
if(isset($qs['category_name']) && isset($qs['paged'])){
$qs['post_type'] = get_post_types($args = array(
'public' => true,
'_builtin' => false
));
array_push($qs['post_type'],'post');
}
return $qs;
}
add_filter('request', 'fix_category_pagination');
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$category = get_category(get_query_var('cat'));
$args = array(
'post_type' => 'shield',
'category_name' => $category->name,
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged);
$cat = new WP_Query( $args );
if( $cat->have_posts() ) :
while( $cat->have_posts() ) :
$cat->the_post();
endwhile;
wp_reset_postdata();
endif;
?>
<div>
<div class="NewsPrev">
<?php previous_posts_link('< Pref',$cat->max_num_pages)?>
</div>
<div class="NewsNext">
<?php next_posts_link('Next >',$cat->max_num_pages)?>
</div>
</div>
If you get 404 try the following:
flush permalinks: Wordpress/settings/permalinks => save changes
permalink structure: Wordpress/settings/permalinks => set to "post name"
wordpress/settings/reading/ => set 'options-reading' WordPress argument to the same as 'posts_per_page' => 10 (not sure if needed for cpt)

Advance Search not showing proper result in Wordpress?

I have added advance search in Wordpress Search.. please check below image..
Below is my search Code in wordpress :
global $wp_query;
$args = array (
's' => $s,
'cat' => $category,
'year' => $year,
'monthnum' => $monthnum
);
$query = new WP_Query($args) ;
<?php if ( $query->have_posts() ) : ?>
<?php while (have_posts() ) : the_post(); ?>
<article>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content( __( 'Read More »', 'tie' ) ); ?>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
From above code i am getting proper result... but when i change the category the result stays same...??
If i get search results as 1,2,3,4,5,6,7,8,9 and on change on category i need 1,5,6,8,9
what is wrong with my code..??
First check your $category is null or not. if it not showing the related to category.
also make sure you $category is integer
See what WP_QUERY use category arguments.
cat (int) - use category id. category_name (string) - use
category slug. category__and (array) - use category id.
category__in (array) - use category id. category__not_in (array)
- use category id.
Change your loop to below code.
For using pagination you may need to change your $args array with the required arguments.
// for number of page.
posts_per_page , paged used for pagination.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = 8; // records per page.
$args = array (
's' => $s,
'cat' => $category,
'year' => $year,
'monthnum' => $monthnum ,
'posts_per_page' => $per_page,
'post_status' => 'publish',
'paged' => $paged,
);
// the query
$query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php if (function_exists("pagination_custom")) {
pagination_custom($query->max_num_pages);
}
?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
UPDATE : For Pagination:
// Pagination code
function pagination_custom($pages = '', $range = 4)
{
$showitems = ($range * 2) + 1;
global $paged;
if (empty($paged)) $paged = 1;
if ($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if (!$pages) {
$pages = 1;
}
}
if (1 != $pages) {
echo "<div class=\"pagination\"><ul>";
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link(1) . "'>« First</a>";
if ($paged > 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link($paged - 1) . "'>‹ Previous</a>";
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
echo ($paged == $i) ? "<li>" . $i . "</li>" : "<li>" . $i . "</li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "Next ›";
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) echo "<a href='" . get_pagenum_link($pages) . "'>Last »</a>";
echo "</ul></div>\n";
}
}

How to add Custom pagination in wordpress

i have created custom theme in wordpress.
I want to add custom pagination to my custom post template which is INDEX.PHP
Can you please check what is wrong in pagination script. actually i have set 4 post limit and there around 8 posts in my blog.. when click on 2 pagination, it won't move to next page...
<?php get_header(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => the_post(),
'posts_per_page' => '3',
'paged' => $paged
);
?>
<?php
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
?>
<div class="row">
<div class="img"><?php the_post_thumbnail('full'); ?></div>
<div class="text">
<h2><?php the_title(); ?></h2>
<h3 style="line-height: 1px;"><span class="floatL">By </span> <span class="floatL"> <?php the_author_posts_link(); ?> </span> <span class="floatL"> - </span> <span class="floatL"><?php the_time('F jS, Y'); ?></span><div class="clr"></div></h3>
<p><?php the_content('Read More') ?></p>
</div>
</div>
<?php
$post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
?>
<div class="row">
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $cquery->max_num_pages
) );
?>
</div>
</div>
<?php get_footer(); ?>
Please help :)
Thank you,
Harshad Patil
Try this
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// WP_Query arguments
$args = array (
'post_type' => 'YOUR_CUSTOM_POST_TYPE',
'posts_per_page' => '3',
'paged' => $paged
);
// The Query
$cquery = new WP_Query( $args );
while ( $cquery->have_posts() ) : $cquery->the_post();
echo $post->ID;
endwhile;
$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' => $cquery->max_num_pages
) );
Add Following code in "functions.php" file:
function custom_pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
And in your "index.php" file add "custom_pagination();" after while loop.

Trying to implement pagination for a specific category ( WORDPRESS )

I implemented a custom post type for a discography.
I managed to edit my template's loop, so it shows all the posts with the "Track" type, but now the pagination is not working anymore.
This is the code in the loop :
<?php
$args = array( 'post_type' => 'Track', 'posts_per_page' => 5);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
//HERE COMES MY HTML STUFF
<?php endwhile;?>
<?php fuse_pagenavi(); ?>
And this is the code for my fuse_pagenavi() :
<?php
function fuse_pagenavi($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span class=\"pageof\">Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"".$i."";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a class=\"pageof\" href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a class=\"pageof\" href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
?>
Thank you in advance !
UPDATE :
Ok I think I make some progress.
I installed the wp-pagenavi plugin and now i see the pagination links at the bottom of the page but they point to mysite.com/?paged=x instead of pointing to mysite.com/?page=x.... i tested manually with 'page' and it works...
The code is the following :
<?php
$paged = get_query_var('page');
$my_query = new WP_Query($args);
$args = array( 'post_type' => 'Track', 'posts_per_page' => 5,'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
//HTML STUFF
<?php
wp_pagenavi(array('query' => $loop));
wp_reset_postdata();
?>
How can I make my pagination links to point to mysite.com?page=x instead of paged=x.
Thanks
try this:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array( 'post_type' => 'Track', 'posts_per_page' => 5, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
//HERE COMES MY HTML STUFF
<?php endwhile;?>
<?php fuse_pagenavi(); ?>
Are you using loop inside of loop. If so, I think this : https://wordpress.stackexchange.com/questions/89191/using-query-posts-inside-single-php-loop is useful for you.
Good luck

Resources