0

I have written a custom wordpress search. The problem is that the pagination is not working.

The pagination (1, 2 , 3 , 4 ,5 >>) appears on the web site, but when i press page 2 (or anything else) no new query data is presented. Its only showing my first page (first 9 products.

Can someone please help me find out what i am doing wrong? :)

please see the web page : http://tevodekk.kamy.no/felgalle/

$display_count = 2;
global $wp_query, $paged;
 $pages = $wp_query->max_num_pages;
//LAGER SØKOUTPUT
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo "paged " . $paged;
$showposts = 10;
    $args = array(
        'posts_per_page' => 20,
        'post_type' => 'any',
        'post__in'      => $WPIDs,
        'orderby' => 'meta_value',
        'meta_key' => 'cf_dimensjon',
        'order' => 'ASC',
        'number'     =>  $display_count,
        'paged'       =>  $paged

);

$wp_query  = new WP_Query( $args );


 //The Loop
while ( $wp_query ->have_posts() ) : $wp_query  ->the_post();
global $product;
//echo $query->request;
if((int)$product->stock > 0 ){

 $url2 = get_permalink( $product_id );
 $url = $product->add_to_cart_url($p);
  $price = get_post_meta( get_the_ID(), '_regular_price', true);
  $stock = get_post_meta( get_the_ID(), '_stock', true);
  $pic = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
  $bredde = array_shift(woocommerce_get_product_terms($product->id, 'cf_felgbredde',               'names'));
    $bredde = get_post_meta(  get_the_ID(), 'cf_felgbredde', true );
    $hoyde = array_shift(woocommerce_get_product_terms($product->id, 'cf_hoyde',       'names'));
    $hoyde = get_post_meta(  get_the_ID(), 'cf_hoyde', true );
    $dim = array_shift(woocommerce_get_product_terms($product->id, 'cf_dimensjon',             'names'));
    $dim = get_post_meta(  get_the_ID(), 'cf_dimensjon', true );
    $innpress = array_shift(woocommerce_get_product_terms($product->id, 'pa_felg-      bredde', 'names'));
    $innpress = get_post_meta(  get_the_ID(), 'cf_innpress', true );
    $produktgruppeny = array_shift(woocommerce_get_product_terms($product->id,             'pa_produktgruppeny', 'names'));


  echo  '<div>
  <div style="float:left;"><a href="'.$url2.'"><img width="64" height="64"       src="'.$pic.'" class="attachment-shop_catalog wp-post-image" alt="Standard bilde -       sommerdekk" style="padding-right: 10px;"></a></div>
        <div style="float:left;width: 390px;padding-top: 12px"><h3><a       href="'.$url2.'">'. the_title( $before, $after, $echo ) .'</a><span class="amount"       style="font-weight: normal;color: #f7f2f7;" >'  . ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;       &nbsp; &nbsp;'   . ' '. '</span></h3>
        <span style="color:#FFFFFF;"> Dim: '.$dim.' Innpress: '.$innpress .  ' Lager       : ' . $stock . ' </span></div>
        <div style="float:left;margin-left: 45px;padding-top: 12px;"><form       class="cart" method="post" enctype="multipart/form-data">
        <div class="quantity buttons_added" style="float: left; margin-right: 11px;">      <input type="button" value="-" class="minus">
        <input type="number" step="1" min="1" name="quantity" value="1"       title="Antall" class="input-text qty text" size="4">
        <input type="button" value="+" class="plus"></div>
        <input type="hidden" name="add-to-cart" value="'.get_the_ID().'">
        <button type="submit" class="single_add_to_cart_button button       alt">Kjøp</button>&nbsp; ' . $price . ' kr ' . '
        </form></div>
      <div style="clear:both;border-bottom: 1px solid #827f82;margin-bottom:       0px;padding-top: 0px;"></div>
    </div>'; }
  //

  endwhile;
  wp_pagenavi();
  • Removed offset and it works great!
6
  • I'm not sure if this is the solution but the first thing I noticed was that you're calculating your $offset using $page which doesn't look to be set in the code above. You probably mean to use $paged - 1.
    – Howdy_McGee
    Oct 22, 2014 at 14:46
  • Thanks, i have updated the offset correctly. Stil the data does not update when i press new page(s)
    – boomdrak
    Oct 22, 2014 at 15:02
  • you can't use offset with paged, the presence of offset causes WP to ignore paged, use one or the other.
    – Milo
    Oct 22, 2014 at 15:24
  • Wow, worked like a charm! :) thanks a million, Milo
    – boomdrak
    Oct 22, 2014 at 15:58
  • 1
    If that's the solution could you post it as an answer below?
    – Tom J Nowell
    Oct 22, 2014 at 16:05

1 Answer 1

0

Working code:

$display_count = 2;
global $wp_query, $paged;
$pages = $wp_query->max_num_pages;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset =  $page - 1;
$showposts = 10;
    $args = array(
        'showposts' => 20,
        'post_type' => 'any',
        'post__in'      => $WPIDs,
        'orderby' => 'meta_value',
        'meta_key' => 'cf_dimensjon',
        'order' => 'ASC',
        'number'     =>  $display_count,
        'paged'       =>  $paged

);

$wp_query  = new WP_Query( $args );

 //The Loop
while ( $wp_query ->have_posts() ) : $wp_query  ->the_post();
global $product;
//echo $wp_query->request;

if((int)$product->stock > 0 ){

 $url2 = get_permalink( $product_id );
 $url = $product->add_to_cart_url($p);
 $price = get_post_meta( get_the_ID(), '_regular_price', true);
$stock = get_post_meta( get_the_ID(), '_stock', true);
$pic = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
$bredde = array_shift(woocommerce_get_product_terms($product->id, 'cf_felgbredde',         'names'));
$bredde = get_post_meta(  get_the_ID(), 'cf_felgbredde', true );
$hoyde = array_shift(woocommerce_get_product_terms($product->id, 'cf_hoyde', 'names'));
$hoyde = get_post_meta(  get_the_ID(), 'cf_hoyde', true );
$dim = array_shift(woocommerce_get_product_terms($product->id, 'cf_dimensjon', 'names'));
$dim = get_post_meta(  get_the_ID(), 'cf_dimensjon', true );
$innpress = array_shift(woocommerce_get_product_terms($product->id, 'pa_felg-bredde', 'names'));
$innpress = get_post_meta(  get_the_ID(), 'cf_innpress', true );
$produktgruppeny = array_shift(woocommerce_get_product_terms($product->id,     'pa_produktgruppeny', 'names'));


echo  '<div>
     <div style="float:left;"><a href="'.$url2.'"><img width="64" height="64"     src="'.$pic.'" class="attachment-shop_catalog wp-post-image" alt="Standard bilde -     sommerdekk" style="padding-right: 10px;"></a></div>
      <div style="float:left;width: 390px;padding-top: 12px"><h3><a     href="'.$url2.'">'. the_title( $before, $after, $echo ) .'</a><span class="amount"     style="font-weight: normal;color: #f7f2f7;" >'  . ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;     &nbsp; &nbsp;'   . ' '. '</span></h3>
      <span style="color:#FFFFFF;"> Dim: '.$dim.' Innpress: '.$innpress .  ' Lager :     ' . $stock . ' </span></div>
      <div style="float:left;margin-left: 45px;padding-top: 12px;"><form class="cart"     method="post" enctype="multipart/form-data">
        <div class="quantity buttons_added" style="float: left; margin-right: 11px;">    <input type="button" value="-" class="minus">
        <input type="number" step="1" min="1" name="quantity" value="1"     title="Antall" class="input-text qty text" size="4">
        <input type="button" value="+" class="plus"></div>
        <input type="hidden" name="add-to-cart" value="'.get_the_ID().'">
        <button type="submit" class="single_add_to_cart_button button     alt">Kjøp</button>&nbsp; ' . $price . ' kr ' . '
        </form></div>
    <div style="clear:both;border-bottom: 1px solid #827f82;margin-bottom:     0px;padding-top: 0px;"></div>
  </div>'; }
//

endwhile;

Your Answer

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

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