I have tried multiple solutions but it's not working.
When I go to /page/2, it doesn't work.
I'm executing a custom query in index.php of my theme.
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' => array('post', 'music', 'videos'),
'post_status' => 'publish',
//'meta_key' => 'featured',
//'meta_value' => '1',
'posts_per_page' => 10,
'orderby'=>'date',
'order'=>'DESC',
'paged' => $paged
);
query_posts($args);
Here is the link to my website: Home Page of my Site
This page is not working (throwing 404) - Page which is not working(of the format - mywebsite/page/2/)
Just realized this page 2 works - Page which is working (of the format - mywebsite.com/?page=2)
I was having the same problem and this fixed everything for me. This allows me to paginate on index.php as well as my page.php with pretty permalinks.
HTML/PHP:
<?php
//Fix homepage pagination
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
$temp = $wp_query; // re-sets query
$wp_query = null; // re-sets query
$args = array( 'post_type' => array('post', 'music', 'videos'), 'orderby'=>'date', 'order'=>'DESC', 'posts_per_page' => 10, 'paged' => $paged);
$wp_query = new WP_Query();
$wp_query->query( $args );
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<!--your loop stuff here -->
<?php endwhile; ?>
<nav>
<?php paginate(); ?>
$wp_query = null;
$wp_query = $temp; // Reset
</nav>
This allows several things. One it checks if your on home, page, or single and tells the $paged variable how to react in turn. It also allows you to query your pagination with custom post types. Also by not using query_post you get to avoid some really funky stuff that you sometimes get when using it. The paginate(); is a custom function which we bring in now:
Inside your functions.php
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' => 'list',
'next_text' => '»',
'prev_text' => '«'
);
if( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', 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 );
}
This originally came from http://bavotasan.com/2011/simple-pagination-for-wordpress/ with me slightly modding it to get the pagination to work on the homepage.
This, again, does several things. It paginates your page with each page getting it's own link (which I find nice) and it also re-writes the URL to allow for pretty permalinks. If you check the link, the variable 's' was being used in place of 'paged' in part of this. I replaced the 's' with 'paged' and everything worked perfectly on my end.
Optional Styling of Pagination
ul.page-numbers {
margin: 20px 0 10px;
width: 100%;
padding: 0;
font-size: 12px;
line-height: normal;
clear: both;
float: left;
}
ul.page-numbers li {
float: left;
}
ul.page-numbers a,
ul.page-numbers span {
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
background: -webkit-gradient(linear, left top, left bottom, from(#E4E3E3), to(#FFFFFF));
background: -moz-linear-gradient(top, #E4E3E3, #FFFFFF);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#E4E3E3', endColorstr='#FFFFFF');
padding: 3px 4px 2px 4px;
margin: 2px;
text-decoration: none;
border: 1px solid #ccc;
color: #666;
}
ul.page-numbers a:hover,
ul.page-numbers span.current {
border: 1px solid #666;
color: #444;
}
Edit
I realized afterwards that home page pagination would break after clicking one of the page tabs. I've fixed this by replacing the conditional statement and putting this in its place. I've updated my code above as well.
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );
I sometimes have trouble getting the pagination to work correctly as well. Try using the query below and see if that helps. I've just added basically one line of code and changed your query to not use the $args
variable.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => array(
'post',
'music',
'videos',
),
'post_status' => 'publish',,
'posts_per_page' => 10,
'orderby'=>'date',
'order'=>'DESC',
'paged' => $paged )
);
if (have_posts()) : while (have_posts()) : the_post();
?>
// Display your content here
<?php the_title ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
A few things here, never change the main query for a custom query on your home page or on any archive page. You'll always have trouble with pagination.
Also, never use query_posts
to create a custom query. My emphasis, like in never.
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
If you need to change the behavior the homepage or any archive page, use pre_get_posts` to change/modify the main query before it is executed. This is the correct way to change the main query. By altering the main query before execution, you'll have no problem with pagination.
pre_get_posts
can be used in conjunction with conditional tags to target specific pages. Have a look at the links provided for examples.
I have been struggling with this problem for a while now, it turned our WordPress was not setting one of the required fields for me, max_num_pages
.
So I set this variable within the loop. Here is my sample code.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('post_type' => 'cpt', 'paged'=>$paged, 'posts_per_page' => 3);
$loop = new WP_Query($args);
$GLOBALS['wp_query']->max_num_pages = $loop->max_num_pages; // This is the line that did the magic for me.
Upon further reading, I found out this;
- On single.php, singular.php and other single view templates:
(int) $page
Is the page of the post, as specified by the query var page: get_query_var( 'page' )
;
- On all sorts of archive.php and similar archive/post type list view templates:
(int) $paged
Is the global variable contains the page number of a listing of posts (as in archives).
This means that if you are on a page template (which is a single view template) and are using a loop, you will need to set this variable.