WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] 404 error on pagination using custom query (6 posts)

  1. ashercharles
    Member
    Posted 1 year ago #

    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.

  2. IMICreation
    Member
    Posted 1 year ago #

    for posts_per_page = 1 the pagination could not work

  3. ashercharles
    Member
    Posted 1 year ago #

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

  4. IMICreation
    Member
    Posted 1 year ago #

    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();

  5. ashercharles
    Member
    Posted 1 year ago #

    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(); ?>
  6. ashercharles
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic