• Resolved Chris Blackwell

    (@chrisblackwell)


    I have a custom query for my homepage on my website comicville.net. The query pulls the latest from both reviews and news posts. For some reason, pagination nor ‘older entries’ links work. Here is my code:

    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $latest = new WP_Query(array(
            'post_type' => array('post', 'review'),
            'posts_per_page' => 10,
            'orderby' => 'date',
            'order' => 'DESC',
            'paged' => $paged
        ));
    ?>
    
    <?php if ($latest->have_posts()) : while ($latest->have_posts()) : $latest->the_post(); ?>

    Any ideas what I’m doing wrong?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Off the top of head, nothing looks wrong but, you know, I’ve been wrong before. Where is the code for inserting the links you mention?

    Thread Starter Chris Blackwell

    (@chrisblackwell)

    This is what I’m currently using:

    <div class="navigation">
        <?php previous_posts_link(); ?> ? <?php next_posts_link(); ?>
    </div>

    [No bumping, thank you.]

    Moderator keesiemeijer

    (@keesiemeijer)

    As you can see pagination does work but the previous_posts_link and next_posts_link don’t seem to work correctly: https://comicville.net/page/2/

    Try it without the query (new WP_Query()) in your index.php and with a normal loop and put this in your theme’s functions.php:

    function my_home_query( $query ) {
      // not an admin page and it is the main query
      if (!is_admin() && $query->is_main_query()){
        // alter query only on the home page
        if(is_home()){
          $query->set('post_type', array('post','review''));
          $query->set('posts_per_page', 10);
    
        }
      }
    }
    add_action( 'pre_get_posts', 'my_home_query' );

    Can you paste and submit the full code of index.php into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Thread Starter Chris Blackwell

    (@chrisblackwell)

    That didn’t seem to work either. What seemed to work was me having to dump the new WP_Query string, and modifying the query_posts instead.

    query_posts( array( 'post_type' => array('review', 'post'),'paged' => get_query_var('page') ) );

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