Support » Developing with WordPress » Pagination not working

  • Resolved kirasiris

    (@kirasiris)


    I was using a custom WP Query and the pagination was working well. Now I try to show the posts in a more dynamic way (deleting the wp query and the while) by using the get_posts (and foreach) and now the pagination is not working :/ . Any idea how to fix this?

    
    <?php $videos_query = get_posts('category_name=videos&posts_per_page=9&order=DESC'); ?>
    <?php  if($videos_query) : foreach ($videos_query as $post) : setup_postdata($post); ?>
    <div class="col-md-4" id="col-md-12-videospage">
    <article>
    <?php if(has_post_thumbnail()) : ?>
    <a href="<?php the_permalink(); ?>" class="thumbnail">
    <?php the_post_thumbnail(); ?>
    </a><?php endif; ?>
    <a href="<?php the_permalink(); ?>"><h4 id="h4enpagevideosandpageportfolio"><?php the_title();?></h4></a>
    </article>
    </div>
    <?php endforeach; ?>
    <?php else : ?>
    <div class="alert alert-danger text-center"><p>Ningun portfolio encontrado</p></div>
    <?php endif; ?>
    </div>
    </div>
    <!-- Pager -->
    <div class="clearfix"></div>
    <ul class="pager">
    <li><?php next_posts_link( 'Older'); ?></li>
    <li><?php previous_posts_link( 'Newer'); ?></li>
    </ul>
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • next_posts_link() & previous_posts_link() will not work with get_posts().

    try this and see if it works

    <?php $videos_query = new WP_Query('category_name=videos&posts_per_page=9&order=DESC'); ?>
    <?php  if($videos_query->post_count) : foreach ($videos_query->posts as $post) : setup_postdata($post); ?>
    <div class="col-md-4" id="col-md-12-videospage">
    <article>
    <?php if(has_post_thumbnail()) : ?>
    <a href="<?php the_permalink(); ?>" class="thumbnail">
    <?php the_post_thumbnail(); ?>
    </a><?php endif; ?>
    <a href="<?php the_permalink(); ?>"><h4 id="h4enpagevideosandpageportfolio"><?php the_title();?></h4></a>
    </article>
    </div>
    <?php endforeach; ?>
    <?php else : ?>
    <div class="alert alert-danger text-center"><p>Ningun portfolio encontrado</p></div>
    <?php endif; ?>
    </div>
    </div>
    <!-- Pager -->
    <div class="clearfix"></div>
    <ul class="pager">
    <li><?php next_posts_link( 'Older'); ?></li>
    <li><?php previous_posts_link( 'Newer'); ?></li>
    </ul>
    <?php wp_reset_postdata(); ?>
    kirasiris

    (@kirasiris)

    I already tried that code and did not work, I tried again as you suggested but again I get the same result. Its not working :/

    Moderator bcworkz

    (@bcworkz)

    Hello again 🙂

    Soumanta is correct about WP pagination functions not working with get_posts(). Pagination relies on certain global factors from the main query. Custom queries such as those made with get_posts() do not affect these factors. The WP pagination is trying to page the main query, not your query. Secondly, your query/get_posts args make no attempt to show the requested page, it’s always the first 9 posts matching the criteria.

    The best solution IMO is to not make a custom query or use get_posts. Rather use the “pre_get_posts” action to modify the main query to get the desired results. Then the pagination functions will work correctly and you don’t need to fuss with your own pagination.

    If you cannot modify the main query for some reason, you need to manage your own pagination. This Codex article will help you properly paginate your query: Making Custom Queries using Offset and Pagination

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    For custom queries you need to provide the second parameter $max_pages in the next_posts_link() function.
    https://codex.wordpress.org/Function_Reference/next_posts_link

    
    <li><?php next_posts_link( 'Older', $videos_query->max_num_pages ); ?></li>
    <li><?php previous_posts_link( 'Newer'); ?></li>
    

    You also need to provide the $paged variable to your query. Here’s an example.
    https://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query

    kirasiris

    (@kirasiris)

    Yes, I actually ended up doing the custom WP_Query that I already had. I was just trying to change things a bit to see if I could get the pagination and the custom posts with less php code.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    I’m glad you’ve got it solved.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Pagination not working’ is closed to new replies.