2

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.

  • 2
    I wonder why you don't use custom post type archive and chose to implement it withing a page template? – Rarst Apr 1 '16 at 15:43
  • This 'custom post type' called 'fltnews' is created through pods. Is this why this doesn't work? Also, I think creating a custom post type would solve this. So my question is now: how can I fix this when using pods? – Anders Stensaas Apr 3 '16 at 14:26
  • 1
    That question might be better aimed at Pods' developers. – Rarst Apr 3 '16 at 14:32
1

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.

  • what happen if you test over limit of your paging? normally it should be 404. – Jevuska Apr 4 '16 at 14:21
  • I can test this and give you back the results. You are correct in that it should give a 404. – Anders Stensaas Apr 4 '16 at 14:44
  • 1
    I mean "normally" if it is still in your custom page template, base on your question, you get 404 page in pagination. By default WP if you use page or custom page template, tested pagination with /page/2, it doesn't create 404 error actually. – Jevuska Apr 4 '16 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.