WP_Query & pagination not working
-
Here’s my code.. it’s driving me mad. I’ve read every support ticket I can and still no cigar..
[Code moderated as per the Forum Rules. Please use the pastebin]
Anyone got any ideas?
-
Might make sure you are using proper pagination. Here’s an example:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('cat=1&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged); ?>
Thanks Michael..
Tried that but with no effect.. here’s the new code (properly stripped back this time)..
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $my_query = new WP_Query(); $my_query->query('category_name=news&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged); ?> <?php if ($my_query->have_posts()) : ?> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>"> <!-- do stuff --> </div><!-- .post --> <?php endwhile; ?> <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">«</span> Older posts', 'thematic')) ?></div> <div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">»</span>', 'thematic')) ?></div> <?php endif; ?>
Any ideas?
ok, I fixed it ..
In the code above you’ll see that I named my new instance of the WP_Query object $my_query.. that’s the problem.
The code in the internal wordpress functions next_posts_link & previous_posts_link expect the query object to be called $wp_query. Once I renamed my object it all started working.
This is confusing since a number of other support tickets around this subject have supposedly working code examples with an object name other than $wp_query ($my_query being an explicit one of these).
Anyhoo.. the working code is..
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $wp_query = new WP_Query(); $wp_query->query('category_name=news&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged); ?> <?php if ($wp_query->have_posts()) : ?> <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>"> <!-- do stuff --> </div><!-- .post --> <?php endwhile; ?> <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">«</span> Older posts', 'thematic')) ?></div> <div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">»</span>', 'thematic')) ?></div> <?php endif; ?>
Hi ..
I am facing the same problem and the above solution is not working for me ??
I m getting 404 page when I click on page 2 or 3 … here is my code
$catID = get_query_var(‘cat’);
$catName = strtolower(get_cat_name($catID));$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$wp_query = new WP_Query();
$wp_query->query(“category_name=$catName&paged=$paged&posts_per_page=1”);<?php if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
// data ..
<?php endwhile; ?>
<?php endif; ?>Any help!
Thanks
Ashish$catID = get_query_var('cat'); $catName = strtolower(get_cat_name($catID)); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $wp_query = new WP_Query("category_name=$catName&paged=$paged&posts_per_page=1"); <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> // data .. <?php endwhile; ?> <?php endif; ?>
Try this.
Thanks for your reply.
I have tried this as well but it is still NOT working ??
More info …
I am on archive.php where I m showing posts related to passed category i.e. (?cat=[num] in the query string)
The default page is coming and showing the first records (I have set post_per_page to 1) and also showing 5 pages in page nav. When I click on page 2/3/4/5 it shows a 404 page ??
Hope I am a bit clear this time.
maybe
new WP_Query("category_name=$catName&paged=$paged&posts_per_page=1");
to:
new WP_Query("category_name=$catName&paged='. $paged .'&posts_per_page=1");
?
see
'&paged=' . $paged
Hello everyone,
I’m also getting a 404 error everytime i click on another page number :S
Here’s my current post query:
<?php query_posts(array('post_type'=>array('post','jogos','videos'),'posts_per_page'=>6, 'paged=>' . $paged )); while (have_posts()) : the_post(); ?>
I tried adding the paged thing but it didn’t work :S
Thanks in advance,
TiagoTHANK YOU SO MUCH!
Just spend 3 hours trying to figure out what was wrong but that fixed it…$catID = get_query_var('cat'); $catName = strtolower(get_cat_name($catID)); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $wp_query = new WP_Query("category_name=$catName&paged=$paged&posts_per_page=1");
THANK kathatp.
But this work only in this private case.
If posts_per_page is change or delete word but with all Categories.I diggin in my head how to fix that because two of objects is not working proper. My decision is…
Put this in function.php
function my_posts_nav_link( $type = '', $label = '', $maxPageNum = '' ) { $args = array_filter( compact('type', 'label', 'maxPageNum') ); echo my_get_posts_nav_link($args); } function my_get_posts_nav_link($args = array()) { $return = ''; $defaults = array( 'maxPageNum' => '0', ); $max_num_pages = $args['maxPageNum']; $args = wp_parse_args( $args, $defaults ); $paged = get_query_var('paged'); if ( $max_num_pages > 1 ) { if ($args['type'] == "next") { $return = get_previous_posts_link($args['label']); } if ($args['type'] == "prev") { $return = my_get_next_posts_link($args['label'], $max_num_pages); } } return $return; } function my_get_next_posts_link( $label = 'Next Page »', $max_page = 0 ) { global $paged, $wp_query; if ( !$paged ) { $paged = 1; } $nextpage = intval($paged) + 1; if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) { $attr = apply_filters( 'next_posts_link_attributes', '' ); return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>'; } }
and call with
my_posts_nav_link('next','Newer',''.$queryObject->max_num_pages.'');
- The topic ‘WP_Query & pagination not working’ is closed to new replies.