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.

I have a blog Post home page with the following code in home.php:

<div id="primary">
<div id="content" role="main">
<h2 class="page-title">Featured Products</h2>
<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
wp_reset_query();
mp_display_products('&post_type=product&paged=' . $paged, $display) ; ?>

<div class="clear index-space"></div>
 </div><!-- #content -->
</div><!-- #primary -->

Now the homepage works just fine, but the ?paged=2 throws an error 404. Don't know why. In products categories the pagination works fine too.

Any help would be appreciated.

EDIT

Here is the code to the function mp_display_products()

function mp_display_products($query = '', $display){

        wp_reset_query();

        //$new_query_wp = new WP_Query($query) ;
        $new_query = query_posts($query) ;


        wp_simple_pagination(/*array('base' => 'store/products')*/);   

        $i = 1 ;

        if($display == 'row'){
                echo '<table summary="Display featured products in rows" id="products-list">' ;
        }

        if ( $new_query != '' ) : ?>

                <?php /* Start the Loop */ ?>
                <?php foreach ($new_query as $post) {

                                $sale_price = get_post_meta($post->ID, 'mp_sale_price', true) ;
                                $sale_price = $sale_price[0] ;
                                $price = get_post_meta($post->ID, 'mp_price', true) ;

                                //Determines end_price
                                if($sale_price == 0){

                                        //End price is normal price
                                        $end_price = '$'.$price[0] ;

                                }
                                else{

                                        //Or end price is  sale price
                                        $end_price = '$'.$sale_price ;
                                }
                                //If theres a sale price
                                if($sale_price != 0){

                                        //We display
                                        $price = 'RRP: <span class="price">$'.$price[0].'</span>' ;

                                }
                                else{

                                        //Or we don't
                                        $price = "" ;
                                }
                        ?>
                        <?php //We display each product

                        //Let's shorten the title string
                        $title = $post->post_title ;
                        $string_size = 25 ;

                        //Test if string is longer that string_size
                        if(strlen($title) > $string_size){

                                $title = substr($title, 0, $string_size).'...' ;

                        }

                        //Check if we need a grid of a row display
                        if($display == 'grid'){

        //Inventory
        $inventory = get_post_meta($post->ID, 'mp_inventory', true) ;

                        ?>
                <article class="post-<?php echo $post->ID ; ?> product post-product-<?php echo $i ; ?>">
                        <h2><a href="<?php echo get_permalink( $post->ID ) ; ?>" title="View product: <?php echo $post->post_title ; ?>"><?php echo $title ; ?></a></h2>

                        <?php //Test if there is a thumbnail
                        if(has_post_thumbnail($post->ID)){

                                echo '<div class="image_product">' ;
                                mp_product_image( $echo = true, $context = 'list', $post->ID, $size = 200 ) ;
                                echo '</div><!--end thumbnail-->' ;

                        }
                        else{

                                echo '<div class="image_product">
                                <img src="'.get_template_directory_uri().'/images/default-product.png" alt="Default image for product" />
                                </div>' ;

                        }
                        ?>
                        <div class="product-details">

                                <span class="product-old-price"><?php echo $price ; ?></span>
                                <span class="product-detail-link"><a href="<?php echo get_permalink($post->ID) ; ?>">Details</a></span>
                                <span class="product-price"><?php echo $end_price ; ?></span>
                        <?php

                        //If inventory isn't empty
                        if(!empty($inventory)){
                                mp_buy_button( $echo = true, $context = 'list', $post_id = $post->ID ) ;
                        }

                                ?>
                        </div> 
                </article>

                        <?php

                        }//end grid display
                        //Start row
                        else{

                                echo '<tr>

                                        <td>' ;
                                        if(has_post_thumbnail($post->ID)){

                                                echo '<div class="image_product">' ;
                                                mp_product_image( $echo = true, $context = 'list', $post->ID, $size = 88 ) ;
                                                echo '</div><!--end thumbnail-->' ;

                                        }
                                        else{

                                                echo '<div class="image_product">
                                                <img src="'.get_template_directory_uri().'/images/default-product.png" alt="Default image for product" />
                                                </div>' ;

                                        }

                                echo '</td>
                                        <td class="title"><h2><a href="'.get_permalink( $post->ID ).'" title="View product: '.$post->post_title.'">'.$post->post_title.'</a></h2>
                                        <span class="product-detail-link"><a href="'.get_permalink( $post->ID ).'">Details</a></span></td>
                                        <td>Customer Rating:' ;
                                        if(function_exists('kk_star_ratings')) : echo kk_star_ratings($post->ID); endif;

                                $comments_count = wp_count_comments( $post->ID );

                                echo '<span class="reviews">Reviews ('.$comments_count->approved.')</span></td>
                                        <td class="price"><span class="product-price">'.$end_price.'</span>';
                                        mp_buy_button( $echo = true, $context = 'list', $post_id = $post->ID ) ;
                                echo '</td></tr>' ;

                        }


                        $i++ ;

                        if($i == 4){ $i = 1 ; }

                        ?>     
                <?php }
                        if($display == 'row'){
                        echo '</table>' ;
                }
                ?>
        <?php else : ?>
        <?php endif; ?>

        <div class="clear index-space"></div>
        <?php
        wp_simple_pagination(array('base' => 'store/products'));
}
share|improve this question
    
is this a secondary query, or are you showing only the product post type on your home page? –  Milo Mar 5 '13 at 3:18
    
I've added a paste of the called function. Hope this helps. –  Got The Fever Media Mar 6 '13 at 2:42

3 Answers 3

To understand why 404 erros occur with pagination, you have to first understand the process WordPress follows when a page is requested.

The query is parsed and the results are queried from the database before the template is loaded. When you create a new query in the template, these results are unrelated to the original query. Whether or not there are additional pages, and how many pages exists, is based on the original default main query, WordPress has no "awareness" of your new custom query in the template.

So the way to fix this is to modify that original query before it is sent to the database via the pre_get_posts action rather than calling query_posts in the template.

function wpa89392_homepage_products( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'product' ) );
    }
}
add_action( 'pre_get_posts', 'wpa89392_homepage_products' );
share|improve this answer

Change the line in which you set the variable $paged as follows:

$paged = (get_query_var('page')) ? get_query_var('page') : 1;

And maybe do a wp_reset_query(); before the mentioned line as well (depending on what else is going on in your theme).

// Edit

Here's what I did (and what worked):

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array (
    'category_name' =>'news',
    'posts_per_page' => 5,
    'paged'=>$paged,
); 
query_posts($args);
while (have_posts()) : the_post();
    the_title(); echo ' &mdash; '; the_content();
endwhile;

Both ?page=2 and /2 appended onto the URL led to the second page. Maybe you could give the exact code you are using for testing?

share|improve this answer
    
thanks for your answer, Ive changed the code and it still gives me a 404 –  Got The Fever Media Mar 5 '13 at 2:41
    
Hm, it works for me. See updated answer. –  t f Mar 5 '13 at 2:58

Homepage is not a page template so you should change get_query_var('paged') to get_query_var('page')

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

to

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
share|improve this answer
    
Please give a short description to why your answer will work and why these changes must be made. As your answer currently stand, it is actually just a comment, not an answer. Please edit your answer. –  Pieter Goosen May 1 at 8:43
    
i've edited my answer with description. –  Behzad G. May 1 at 8:52

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.