• Resolved ashercharles

    (@ashercharles)


    I have a custom query which calls all “coupons” in the current category, however when I go to page 2 I get a 404 error.

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    
    	query_posts(array(
    
    			'post_type' => 'coupons',
    			'category__in' => array(get_the_category('cat')),
    			'posts_per_page' => 1,
    			'paged'=> $paged
    
    		)); ?>

    I have tried every single fix I could find in 3 days of searching the web, nothing as worked, when I change the default posts per page setting under reading to 1 then I get a second page working but 404 error on page 3 and beyond. I am absolutely stuck, I have used queries like this before with no issues with pagination.

Viewing 5 replies - 1 through 5 (of 5 total)
  • for posts_per_page = 1 the pagination could not work

    Thread Starter ashercharles

    (@ashercharles)

    Thanks for the reply, however changing posts_per_page to any number still comes up with the 404 error

    try with this
    <?php
    query_posts(array(

    ‘post_type’ => ‘coupons’,
    ‘category__in’ => array(get_the_category(‘cat’)),
    ‘posts_per_page’ => 1,
    ‘paged’=> $paged,
    ‘paged’ => get_query_var(‘paged’)));
    ?>

    and after endwhile reset the query with wp_reset_query();

    Thread Starter ashercharles

    (@ashercharles)

    Still no luck this is what I have so far

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array(
    			'post_type' => 'coupons',
    			'category__in' => array(get_the_category('cat')),
    			'posts_per_page' => 1,
    			'paged'=> $paged,
    			'paged' => get_query_var('paged')
    
    		)); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    // loop content
    
    <?php endwhile; ?>
    <?php posts_nav_link('    ','← Newer Posts','Older Posts →'); ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
    Thread Starter ashercharles

    (@ashercharles)

    Just sorted it out by adding this to my functions.php

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','coupons','savingstips');
    	    $query->set('post_type',$post_type);
    	return $query;
        }
    }

    Where ‘coupons’ and ‘savingstips’ were my custom posts I can now query and use pagination

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘404 error on pagination using custom query’ is closed to new replies.