So I have this blog that filters by category Then by city. Somewhere while creating the filtration I broke the pagination which seemed to work fine earlier. It doesn't work on my homepage where it should show all posts and goes to 404 in my archives. I'm using a custom link structure of /%category%/%postname%/ and in my index page I have :

<p align="center" id="space">
<?php posts_nav_link(' - ',__('&#171; Newer Posts','ml'),__('Older Posts &#187;','ml')) ?>

It is the same thing on my Archives page. I created a function to filter everything by and this is where the paging should happen:

From.php http://pastebin.com/d9jikfPq

    $testParent = $wp_query->get_queried_object();

function cust_query($city, $cat)// Filter and page everything by city/cat
    $setCity = false;
    $setCat = false;

    $paged = get_query_var('paged') ? get_query_var('paged') : 3;               // Seperates Posts into Pages
    $citySlug = get_category_by_slug($city);                                    // Used to get City && Category CatID
    $categorySlug = get_category_by_slug($cat);                                 // ^^^

    if(isset($cat) && $cat != null)                                             // Check if $cat is set
        $category = $categorySlug->cat_ID;                                      // Setting Category CatID
        $setCat = true;
    if(isset($city) && $city != null)                                           // Check if $city is set
        $city = $citySlug->cat_ID;                                              // Setting City CatID   
        $setCity = true;

    if($setCity && $setCat && !$testParent->parent)                             // If both cat and city are set filter by both
           For more information.
            $args = array(
                'tax_query' => array(
                    'relation' => 'AND',
                        'taxonomy' => 'category',
                        'field'    => 'id',
                        'terms'    => $city,
                        'taxonomy' => 'category', 
                        'field'    => 'id',
                        'terms'    => $category,
            $args = array_merge( $args, array( 'paged' => $paged ) );
            return query_posts($args);                                          // Return Query
        return query_posts(array(category__and => array($category, $city), 'paged' => $paged)); // Return Query
    if($setCity)                                                                // Filter by City Only
        return query_posts('cat='.$city.'&paged='.$paged);                      // Return Query
    else if($setCat)                                                            // Filter by Category Only
        return query_posts('cat='.$category.'&paged='.$paged);                  // Return Query
        return query_posts('paged='.$paged);                                    // Return all posts

Header.php http://pastebin.com/vzm08E40

<?php require_once('from.php'); ?>
    $path = $_SERVER['REQUEST_URI'];                        // Get URL and Set it to $path
    $dir = explode(DIRECTORY_SEPARATOR, $path);             // Seperate URL by Directory '/'

    if(isset($_SESSION['city']))                            // City Reset, Remove if statement if you don't want to reset it.               
        unset($_SESSION['city']);                           // add && is_home() to reset on banner click. Useful

    if($dir[4] == 'city')
        $_SESSION['city'] = $dir[5];                        // If sub-Directory is City, get Final Directory as City
        $_SESSION['cat'] = end($dir);                       // Get Category

    $navCity = catName($_SESSION['city']);                  // Get formatted City Name for Navigation
    $navSlug = get_category_by_slug($_SESSION['city']);     // Get Category by Slug
    $cityNavID = $navSlug->cat_ID;                          // Get Category ID by Category
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    <title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <link rel="stylesheet" type="text/css" media="print" href="<?php bloginfo( 'stylesheet_directory' ); ?>/print.css" />
    <link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> <?php _e('RSS Feed','ml');?>" href="<?php bloginfo('rss2_url'); ?>" />
    <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    <?php wp_head(); ?>
<body id="section-index"  <?php body_class(); ?>>
<div id="container">
    //echo $_SESSION['cat'] . " " . $_SESSION['city'];
<div id="header">
<a href="<?php bloginfo('siteurl');?>" title="<?php bloginfo('name');?>">
    <img src="<?php echo get_template_directory_uri() . "/img/logo.jpg" ?>" title="" alt="" />
</div><!-- end id:header -->
    <!-- This Appears Above The Sidebar-->
    <div id="headerSocial">
        <a href="AboutUs.php"> About Us</a> |
        <a href="contact_us.php">Contact Us</a> |
        <a href="member.php">Get Listed</a><br>
        <!-- AddThis Button BEGIN -->
        <div class="addthis_toolbox addthis_default_style ">
            <a class="addthis_button_preferred_1"></a>
            <a class="addthis_button_preferred_2"></a>
            <a class="addthis_button_preferred_3"></a>
            <a class="addthis_button_preferred_4"></a>
            <a class="addthis_button_compact"></a>
            <font color="004682"> &nbsp; Share This!</font>
        <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4e5515692839a404">
        </script><!-- AddThis Button END -->
    <!-- End Header Social -->
<div id="navContainer">
    <div id="nav">
            <li id="cityItem">
                Filter By: <a href="<?php get_category_link($cityNavID); ?>"><?php isset($_SESSION['city']) ? print($navCity) : print("Home") ?></a>
                <ul id="cityChildren">
                    <?php if(isset($_SESSION['city'])) : ?>
                        <li class="cat-item">
                            <?php $cat = get_category_by_slug($_SESSION['cat']); ?>
                            <a href="<?php echo esc_url(get_category_link($cat->cat_ID)) ?>" title="">All</a>
                    <?php endif ?>
                    <?php wp_list_categories('orderby=id&child_of=1&title_li='); ?>
            <?php wp_list_categories("orderby=slug&exclude=1&hide_empty=0&title_li="); ?>
    <?php if(!is_single()) : ?>
        <div id="crumbs">
            <?php include('breadcrumbs.php'); ?>
    <?php endif; ?>

The function above gets passed city / category that needs to be filtered and then depending on if both are set or only one it will filter and page. Except it doesn't page... Any suggestions on what this could be? I also want to point out that my index file does not have a loop it just uses query_posts() and changing the number inside $paged = get_query_var('paged') ? get_query_var('paged') : 3; to anything except 1 puts all my pages to 404. If I make it 1 then it shows all posts and pages index fine... Why is this and what can I do to prevent this from happening?

2 Answers 2


The posts_per_page refers to how many posts per page, and not what page you are viewing. For that, you want to set the paged argument. See the Codex on WP_Query and this article by Scribu.

With the paged argument set, it'll return the appropriate posts depending on the page number (the first x posts for page 1, the next x posts for page 2 etc). If not, you are always querying the first x posts- hence the same for each page.

For instance, instead of

query_posts($args, 'posts_per_page='.$paged);


$args = array_merge( $args, array( 'paged' => $paged ) );

and instead of



  • I tried setting all my 'posts_per_page' to just '&paged='.$paged but it crashed all my pages, they all lead to 404.
    – Howdy_McGee
    Commented Feb 6, 2012 at 17:36
  • I mean, replace posts_per_page= with paged=. I shall update my answer to demonstrate. Commented Feb 6, 2012 at 17:39
  • Right, this might work but for some reason when I add 'paged' it breaks one of my sessions. I'll have to get back to you if it works.
    – Howdy_McGee
    Commented Feb 6, 2012 at 18:03
  • You should probably change $paged = get_query_var('paged') ? get_query_var('paged') : 2; to be 1 rather than 2 by default. If $paged isn't set, it'll show page 2, and if there isn't a second page, that'll give you the 404s. Commented Feb 6, 2012 at 18:07
  • Ok, posts show up now (this is so weird...) but now my $paged var doesn't work, like it's being overrode somewhere, it's set to 1 but it's showing all my posts and when I change it to something like 3 it goes straight to 404...
    – Howdy_McGee
    Commented Feb 6, 2012 at 18:17

Replace the ('posts_per_page='.$paged) with ('paged='.$paged)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.