Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

pagination links not working in custom WP_Query WordPress

  • SOLVED

Hi, I'm using a WP_query to retrieve posts with a meta key.

This is retrieving all my post with the most likes from the '[[LINK href="http://wordpress.org/extend/plugins/i-like-this/"]]i like this[[/LINK]]' plugin.

But my pagination links are not working and I can't get them to work properly when I try amending the query.

See below my query...



<?php

/**

* Template Name: Most Loved

* @package WordPress

* @subpackage XXX

*/

get_header(); ?>


<?php $mostloved = new WP_Query(array(

'order' => 'DESC',

'orderby' => 'meta_value',

'meta_key' => '_liked',

'paged' => $paged,

'posts_per_page' => '1'

) ); ?>

<?php if ( $mostloved->have_posts()) : while ($mostloved->have_posts()) : $mostloved->the_post(); ?>



// Most Loved Post's Loop contents here... >



<?php endwhile; ?>

<?php next_posts_link(__('MORE LOVED')) ?>

<?php previous_posts_link(__('LESS LOVED')) ?>

<?php endif; ?>


<?php get_footer(); ?>




Can any one help me to get the pagination to work?

Thanks

Answers (3)

2011-12-03

Hai Bui answers:

I already sent you a PM about this problem. You didn't receive it?
posts_per_page must be an integer, so don't put single quote ' around it. Try this

<?php $mostloved = new WP_Query(array(
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => '_liked',
'paged' => $paged,
'posts_per_page' => 1
) ); ?>


Hai Bui comments:

or try using this code, it's cleaner than using WP_Query

<?php query_posts(array(
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => '_liked',
'paged' => $paged,
'posts_per_page' => 1
) ); ?>
<?php if ( have_posts()) : while (have_posts()) : the_post(); ?>
// Most Loved Post's Loop contents here
<?php endwhile; ?>
<?php next_posts_link(__('MORE LOVED')) ?>
<?php previous_posts_link(__('LESS LOVED')) ?>
<?php endif; ?>


Josh Cranwell comments:

Nah, sorry dude missed it.

But the normal query above worked a charm. Thanks for help!

2011-12-03

Jurre Hanema answers:

You can't just use an undefined variable (like $paged) like that and expect things to work.

Try it this way:


'paged' => get_query_var('paged') ? get_query_var('paged') : 1,


Josh Cranwell comments:

Thanks! also did the trick with doing what Luis said below

2011-12-03

Luis Abarca answers:

Change to this

<?php next_posts_link(__('MORE LOVED'), $most_loved->max_pages) ?>
<?php previous_posts_link(__('LESS LOVED'), $most_loved->max_pages) ?>


And also change the paged value to Jurre Hanema said.


<?php
$mostloved = new WP_Query(array(
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => '_liked',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'posts_per_page' => 1
) );
?>


Josh Cranwell comments:

Thanks Luis! This helped too!