WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a blog, I am using next_post_link() and prev_post_link() extensively on posts.

I also have a number of pages that I show in a WP3 menu. They are organized in topics and sub-topics e.g. like so:

- Subpage 1
- Subpage 2 
- Subpage 3 

I would like to set up a "previous" and "next" navigation on these pages as well, so that when I'm on "About"'s Subpage 2, I can navigate to Subpages 1 and 3 using the "next" and "previous" arrows.

The default behaviour of next_post_link()/prev_post_link() doesn't work for this: It calculates the next and previous items based on the list of all pages.

Is there an easy way to make next_post_link() and prev_post_link() sensitive to my menu structure, or will I have to build this myself?

share|improve this question
up vote 3 down vote accepted

The function behind the function behind the function…

next_post_link(); is a wrapper for adjacent_post_link(); which then calls get_adjacent_post(); where the 3rd argument is $previous and set to true/false to get the previous or next post.

This gives you two opportunities: Use the output or the query filter.

Output filter

This might sound easier, but you'll have to do an extra query for this.

// The core:
$adjacent = $previous ? 'previous' : 'next';
echo apply_filters( "{$adjacent}_post_link", $format, $link );

// Add your callbacks like this:
add_filter( 'previous_post_link', 'your_callback_fn', 20, 2 );
add_filter( 'next_post_link', 'your_callback_fn', 20, 2 );

Query filter

This is what I'd recommend you: Check whether there's $post->post_parent set and then modify the query to get the post with the next higher id that has the same parent.

// The core:
$adjacent = $previous ? 'previous' : 'next';
$join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
$sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
share|improve this answer
made me laugh "The function behind the function behind the function…" I was just looking at this yesterday for another question on WPSE. I took one look at the queries and went "Hell no!" – Brady Feb 11 '12 at 13:55
:) About the query: Shouldn't be too hard. AND p.post_parent = %s where %s then is $post->post_parent or something close to that. – kaiser Feb 11 '12 at 14:11
Cool. Thanks very much, this gives me a starting point! – Pekka 웃 Feb 12 '12 at 12:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.