Pagination on custom wp_query in WordPress takes to 404 error page
Asked Answered
F

3

6

Im have a loop with wp_query with the following code:

<?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query("showposts=2&paged=$paged");
?>

<?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <?php the_title() ?>
<?php endwhile; ?>
<?php else: ?>
    <article>
        <h2><?php _e( 'Sorry, nothing to display.', 'theme' ); ?></h2>
    </article>
<?php endif;  my_pagination(); wp_reset_query()?>

with standard pagination :

<?php 
function my_pagination()
{
    global $wp_query;
    $big = 999999999;
    echo paginate_links(array(
        'base' => str_replace($big, '%#%', get_pagenum_link($big)),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'prev_text'    => __('<i class="fa fa-chevron-left"></i>'),
        'next_text'    => __('<i class="fa fa-chevron-right"></i>'),
        'total' => $wp_query->max_num_pages,
    ));
}
?>

The pagination is showing correctly on the page, but whenever I click on the pagination link it takes me to the error page.

Tried everything now and have no idea what can be the reason for it.

Amy help much apprecieated

Formation answered 12/3, 2014 at 21:31 Comment(7)
When you hover over the pagination link for page 2 what is the URL?Opheliaophelie
localhost/web/main/blog/page/2 ThanksFormation
So your pagination output looks good. The problem must be with your actual query. Can you try removing $wp_query->query("showposts=2&paged=$paged"); then see if you can access page 2Opheliaophelie
@henrywright, Ive done that bit it is then returning an empty loop. Whats interesting is that when I change Setting->Reading to smaller amount is seem to take impact on my loop. Ive set there 5 items per page. There is only 7 posts and the pagination is showing 3 pages. Seems that it is ignoring wp_query but the third page is showing 404. Im lost now.Formation
If you remove the code I suggested: $wp_query->query("showposts=2&paged=$paged"); and change $wp_query = new WP_Query(); to $wp_query = new WP_Query( 'posts_per_page=2' ); does that return some results? With that in place, your pagination should workOpheliaophelie
Done that, thank you. What happens now is: I have 7 posts. wp_query set to recommended by you. $wp_query = new WP_Query( 'posts_per_page=2' ); if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); The pagination is showing 4 pages which seems to be correct. Settings->Reading is set to show 5 per page. Now, when I click on page 2 is showing me next 2 posts but the pagination is still highlighting the first page. When clicked on third page it takes to to 404. Where can I be wrong?Formation
Do you have any other queries going on? I mean, in what template is your WP_Query happening, can you show it all?Weakness
A
15

Had a hard time with it too :) Was easier to search when I realized it's wrong calculated post per page number, and here is a magic trick: (to be added to functions.php)

function my_post_count_queries( $query ) {
  if (!is_admin() && $query->is_main_query()){
    if(is_home()){
       $query->set('posts_per_page', 1);
    }
  }
}
add_action( 'pre_get_posts', 'my_post_count_queries' );
Arctogaea answered 17/3, 2014 at 21:37 Comment(4)
This is marvelous that they kept this much of backdoors to infiltrate the output and thata why i love wp ;) +1Lisandra
It shows pagination link but after clicking the link display 404. Plz help me.Urbanity
This helped me thank you!! I searched for so many answers and this one works if I take posts_per_page out of my WP_Query and stick it here instead!!Philine
It does not work for "s" to search with keyword $query->set('s',$_GET["search"]'); here it give 404 error. can you please suggest.Proclitic
E
5

Had same problem with custom post type. I had a query on 'page-template' where the pagination came with 404. I guess the main problem here is the 'slug' of custom post type identical to 'page-template' url. For example if you have a custom post type slug 'portfolio' and a page with the same name, pagination on that page gives a 404. So I just changed 'slug' to 'archives-portfolio' and it helped

Eventual answered 2/10, 2016 at 19:1 Comment(1)
Best answer i found :) Thanks !!Roaring
G
0

you can Change your query

$wp_query= null; $wp_query = new WP_Query();
$wp_query->query("showposts=2&paged=$paged");

to

$wp_query = new WP_Query("showposts=2");

It's shown 2 Post per page and you can see and access your page 2.

Grantley answered 21/3, 2014 at 21:12 Comment(1)
As per the official docs: codex.wordpress.org/Class_Reference/WP_Query ... posts_per_page (int) - number of post to show per page (available since Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the 'paged' parameter if pagination is off after using this parameter. Note: if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. To reimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1Essieessinger

© 2022 - 2024 — McMap. All rights reserved.