Support

Account

Home Forums Front-end Issues On single.php: get prev and next post links in the order of custom field

Solved

On single.php: get prev and next post links in the order of custom field

  • I have a archive page with this arguments for the loop,
    the posts are orderd by a custom field and by its title.

    $args = array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘post’,
    ‘meta_key’ => ‘year’,
    ‘orderby’ => array( ‘meta_value_num’ => ‘DESC’, ‘title’ => ‘ASC’ )

    );

    Now I am on single.php and i need to create links to the next and to the previous post in the same order, how can i do this?
    I want to skip through all posts (not category related),

    thanks for any help!

  • Rather than creating a custom query to get the posts you should look at pre_get_posts https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts. I believe that if you use this hook to create a filter that the next_post_link() and previous_post_link() functions will return the correct posts.

  • Sorry i was to fast to select your answer as the solution.

    unfortunately pre_get_posts does not work with previous_post_link and next_post_link as far as i can say at the moment. It seems that maybe get_adjacent_post will do the trick but how do i have to write a get_adjacent_post function that will work with meta_key and orderby?

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘On single.php: get prev and next post links in the order of custom field’ is closed to new replies.