Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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')) ?>
</p>

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
    {
        /* 
           http://wordpress.stackexchange.com/questions/40815/custom-queries-child-categories-not-showing-up
           For more information.
        */
        if(!$testParent->parent)
        {
            $args = array(
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'category',
                        'field'    => 'id',
                        'terms'    => $city,
                    ),
                    array(
                        '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
    else                                                                        
        return query_posts('paged='.$paged);                                    // Return all posts
}

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

<?php require_once('from.php'); ?>
<?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
    else
        $_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">
<head>
<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(); ?>
</head>
<body id="section-index"  <?php body_class(); ?>>
<div id="container">
<?php 
    //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="" />
</a>
</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>
        </div>
        <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4e5515692839a404">
        </script><!-- AddThis Button END -->
    </div>
    <!-- End Header Social -->
<div id="navContainer">
    <div id="nav">
        <ul>
            <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>
                        </li>
                    <?php endif ?>
                    <?php wp_list_categories('orderby=id&child_of=1&title_li='); ?>
                </ul>
            </li>
            <?php wp_list_categories("orderby=slug&exclude=1&hide_empty=0&title_li="); ?>
        </ul>
    </div>
    <?php if(!is_single()) : ?>
        <div id="crumbs">
            <?php include('breadcrumbs.php'); ?>
        </div>
    <?php endif; ?>
</div>

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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

use

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

and instead of

query_posts('posts_per_page='.$paged);

use

query_posts('paged='.$paged);
share|improve this answer
    
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 Feb 6 '12 at 17:36
    
I mean, replace posts_per_page= with paged=. I shall update my answer to demonstrate. –  Stephen Harris Feb 6 '12 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 Feb 6 '12 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. –  Stephen Harris Feb 6 '12 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 Feb 6 '12 at 18:17

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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