logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
pagination links not working in custom WP_Query

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 'i like this' 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

This question has been answered.

Josh Cranwell | 12/03/11 at 9:45am Edit

Previous versions of this question: 12/03/11 at 9:59am

(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/03/11
    10:02am
    Hai Bui says:

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

    • 12/03/11 10:14am

      Hai Bui says:

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

    • 12/03/11 10:42am

      Josh Cranwell says:

      Nah, sorry dude missed it.

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

  • avatar
    Last edited:
    12/03/11
    10:04am
    Jurre Hanema says:

    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,

    • 12/03/11 10:46am

      Josh Cranwell says:

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

  • avatar
    Last edited:
    12/03/11
    10:31am
    Luis Abarca says:

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

    Previous versions of this answer: 12/03/11 at 10:31am

    • 12/03/11 10:48am

      Josh Cranwell says:

      Thanks Luis! This helped too!

This question has expired.



Josh Cranwell, Julio Potier, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.