Pagination gives 404 in template

I have a page in ‘Pages‘ called ‘News‘. This page has a template containing my paged custom WP_Query.

Here’s the code:

<?php

    $paged = 1;
    if (get_query_var('paged')) {
      $paged = get_query_var('paged');
    } elseif (get_query_var('page')) {
      $paged = get_query_var('page');
    }

    $arguments = array (
        'posts_per_page' => 3,
        'post_type' => 'fltnews',
        'paged' => $paged,
        'meta_key' => 'publish_date',
        'orderby' => 'meta_value',
        'order' => 'DESC'
    );

    $custom_wp_query = new WP_Query($arguments);
    $temp_wp_query  = $wp_query;
    $wp_query       = NULL;
    $wp_query       = $custom_wp_query;

    echo "<ul class='content-list'>";
    if ($custom_wp_query->have_posts()) :
        while ($custom_wp_query->have_posts()) : $custom_wp_query->the_post();
            echo "<li class='content-list-item'>";
            $podsForNewsItem = pods('fltnews', $post->ID);
            $podsImage = pods_image ( $podsForNewsItem->display( 'thumbnail_picture'), 'full');
            if ($podsImage != ""){
                $podsImage = "<div class='content-list-thumbnail-picture'>" . $podsImage . "</div>";
            }

            printf("<a href='%s' class='item-container'>%s
                            <div class='content-list-body'>
                                <div class='content-list-publisher'>
                                    <div class='date-block'><span class='date'>%s</span></div>
                                </div>
                                <div class='content-list-title'>%s</div>
                                <div class='content-list-text'>%s</div>
                                <div class='content-list-link'>Les mer</div>
                            </div>
                        </a>",
                    esc_url(get_permalink($post->ID)),
                    $podsImage,
                    getDateAsNorwegian($podsForNewsItem->field('publish_date')),
                    $post->post_title,
                    $post->post_excerpt);

            echo "</li>";
        endwhile;

        wp_reset_postdata();

        previous_posts_link('Nyere &raquo;');
        next_posts_link('&laquo; Eldre nyheter', $custom_wp_query->max_num_pages);

        $wp_query = NULL;
        $wp_query = $temp_wp_query;

    endif;
    echo "</ul>";
?>

I’ve looked at the following questions and answers:

It seems like everyone always fixes the 404 problem when adding the ‘paged‘ key and value to the WP_Query. However, this doesn’t work for me.

The page ‘News‘ is at the following URL: /news.
The next_posts_link gives the following link: /news/page/2.

This is the one giving me the 404.
My Permalink Settings is as follows: Day and name.

I don’t understand why I’m getting the 404.

Solutions Collecting From Web of "Pagination gives 404 in template"

I did what Rarst told me to do. First I searched around in the Pods Framework documentation. This solved my problem.

Here’s what the code looks like right now.

<?php

  $params = array(
    'orderby' => 'publish_date.meta_value DESC',
    'limit' => 10
  );

  $fltNewsPods = pods( 'fltnews', $params );
  while ($fltNewsPods->fetch()) {
    echo "<li class='content-list-item'>";
    $podsForNewsItem = pods('fltnews', $fltNewsPods->display( 'id' ));
    $podsImage = pods_image ( $podsForNewsItem->display( 'thumbnail_picture'), 'full');
    if ($podsImage != ""){
        $podsImage = "<div class='content-list-thumbnail-picture'>" . $podsImage . "</div>";
    }

    printf("<a href='%s' class='item-container'>%s
                          <div class='content-list-body'>
                              <div class='content-list-publisher'>
                                  <div class='date-block'><span class='date'>%s</span></div>
                              </div>
                              <div class='content-list-title'>%s</div>
                              <div class='content-list-text'>%s</div>
                              <div class='content-list-link'>Les mer</div>
                          </div>
                      </a>",
                  esc_url(get_permalink($podsForNewsItem->display( 'id' ))),
                  $podsImage,
                  getDateAsNorwegian($podsForNewsItem->field('publish_date')),
                  $podsForNewsItem->display( 'title' ),
                  $podsForNewsItem->display( 'excerpt' ));
      echo "</li>";
  }

  echo $fltNewsPods->pagination( array(
    'type' => 'paginate',
    'prev_next' => false,
    'first_last' => false
    ) );

  echo "</ul>";
?>

It’s basically the same, although high use of the pods framework.

I guess this didn’t work with the WP_Query version because this wasn’t a custom post type, so it wasn’t using the WordPress Framework. WordPress wouldn’t know how these should be paginated.