Forums

[resolved] Previous/Next Posts Links broken when sorting by meta_key (5 posts)

  1. toneburst
    Member
    Posted 2 years ago #

    I've just been trying to change the sort-order of my posts so I can sort by a custom field value. I've got sorting working, using

    <?php query_posts('meta_key=student_secondname&orderby=meta_value&order=ASC') ?>

    but now the Previous/Next Posts links don't work. If there are more posts than can be displayed on the page, the Previous/Next links are there, but if I click them, I get the same list of posts, and only ever see the first set.

    Is this a known bug in WordPress (I'm using the latest version), or is it more likely a bug in the template code somewhere?

    The template uses the standard WP 'previous_posts_link' and 'next_posts_link' keywords.

    Anyone any thoughts on what might be going wrong, and if there's anything I can do about it?

    a|x

  2. esmi
    Theme Diva & Mod
    Posted 2 years ago #

    Using query_posts means that you lose the default query values - including those for paging, so you need to re-insert them as part of the custom query. Try:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'meta_key' => 'student_secondname',
    	'orderby' => 'meta_value',
    	'order' => 'ASC'
    	'paged' => $paged
    );
    query_posts($args);
    ?>
  3. toneburst
    Member
    Posted 2 years ago #

    Hi esmi,

    thanks for getting back to me so quickly!

    That all makes perfect sense, and seems to work fine on the Index page. Unfortunately, it does seem to work on the Archive page, for some reason.

    This is the code I'm using on the Archive page:

    <!-- Change post sort-order -->
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $queryArgs = array(
    	'cat' => $cat,
    	'meta_key' => 'student_secondname',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	'paged' => $paged
    );
    query_posts($queryArgs); ?>

    I'm not sure if it's a problem with paging itself though. I'm not completely sure how to ensure that a particular set of posts should be paged, and also if the paging 'previous/next' links can be hidden if pagination isn't necessary for a particular collection of posts (ie because there are fewer of them than the value of 'posts_per_page).

    Thanks again,

    a|x

  4. esmi
    Theme Diva & Mod
    Posted 2 years ago #

    If you use something like:

    <?php previous_posts_link(__('Newer Posts') );?> <?php next_posts_link(__('Older Posts') ); ?>

    WP will take care of the next/previous links for you - including "hiding" the links if pagination isn't necessary.

  5. toneburst
    Member
    Posted 2 years ago #

    Oh, now it seems to work. I'm not sure what I've changed, but it wasn't working before....

    Thanks again :)

    a|x

Topic Closed

This topic has been closed to new replies.

About this Topic