Support » Developing with WordPress » Pagination with GET parameters giving 404 error

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Your page uses a custom query. That’s the problem. WP thinks you are trying to paginate the main query, of which there is only one page, so there’s nothing to paginate. Thus getting the paged query var will not work because it belongs to the main query, of which there is only one page.

    You can either manage your own pagination query var, extracting it directly from the request string, or alter the main query through “pre_get_posts” so it outputs the posts you want. Then the default pagination of the archive template used will work.

    sushanttayade123

    (@sushanttayade123)

    Thanks @bcworkz for your explanation, but I’m still confused. Below I have given template code. Can you suggest me the way to achieve this functionality.

    
    <?php
    /*
     * Template Name:  All Colleges list
      Template Post Type: cb_college
     */
        get_header();
    
      //wp_reset_query();
        $args = array(
            'post_type' => 'cb_college',
            'posts_per_page' => 2,
            'orderby'=> 'title',
            'order' => 'ASC',
            'post_status' => 'publish',
            'paged' => get_query_var('paged') ? get_query_var('paged') : 1
        );
    
        if( isset($_GET['filter_submit']) || isset($_GET['location']) ){
            $tax_query = array('relation' => 'AND');
            if (isset($_GET['location']))
            {
                $tax_query[] =  array(
                    'taxonomy' => 'location',
                    'field' => 'name',
                    'terms' => $_GET['location']
                );
            }
            $args['tax_query'] = $tax_query;
        }
     
        $wp_query = new WP_Query( $args );
        if (have_posts()) : while ($wp_query->have_posts() ) : $wp_query->the_post();
            ?>
            <h5 style="text-align:left;"><?php echo  get_the_title() ; ?></h5>
            <?php   // Get terms for post
                $terms = get_the_terms( $post->ID , 'location' );
                // Loop over each item since it's an array
                if ( $terms != null ){
                    foreach( $terms as $term ) {
                        // Print the name method from $term which is an OBJECT
                        echo "<p><b>Location : </b>".$term->name."</p>";
                    }   
                }
            endwhile ; 
            
            // pagination
            next_posts_link();
            previous_posts_link();
        endif ;
        wp_reset_query();
        get_footer(); 
    ?>
    
    Moderator keesiemeijer

    (@keesiemeijer)

    You can set how many pages to use with the next_posts_link()
    https://developer.wordpress.org/reference/functions/next_posts_link/#comment-1297

    
            // pagination
            next_posts_link( __( 'Older Entries', 'textdomain' ), $wp_query->max_num_pages );
            previous_posts_link();
    

    Also, change this

    
        if (have_posts()) : while ($wp_query->have_posts() ) : $wp_query->the_post();
    

    To this

        if ($wp_query->have_posts()) : while ($wp_query->have_posts() ) : $wp_query->the_post();
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.