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

pagination to next page with the same parent WordPress


i need a function to go to the next page (page, not post) which have the same parent
and a function to go to the previous page which have the same parent

I will use this function in page.php

Answers (3)


Ivaylo Draganov answers:


you can use <em>adjacent_post_link</em> function to print the links to the previous/next page by date:

adjacent_post_link('%link', '%title'); // the previous page
adjacent_post_link('%link', '%title', false, '', false); // the next page

I've not yet tested how that behaves with subpages. I'll make a test and come back later.

jevusi comments:

i want display only the pages whiche have the same parent.

Ivaylo Draganov comments:

Yes, I understand your goal. I am currently working on that :)

Ivaylo Draganov comments:

I think I've cracked it. Put this in your <em>functions.php</em>:

// function to filter the where clause in the query for adjacent posts
function sql_get_adjacent_post_where( $previous = true ) {

global $post, $wpdb;

// set SQL comparison operator
$op = $previous ? '<' : '>';

// build and return where clause
return $wpdb->prepare("WHERE p.post_date {$op} %s AND p.post_type = %s AND p.post_status = 'publish' AND p.post_parent = %s", $post->post_date, $post->post_type, $post->post_parent);


// previoust post where clause
function sql_get_previous_post_where() {
return sql_get_adjacent_post_where();

// next post where clause
function sql_get_next_post_where() {
return sql_get_adjacent_post_where( false );

And the use this to print the links in your <em>page.php</em>:

// filter queries
add_filter( 'get_previous_post_where', 'sql_get_previous_post_where', $in_same_cat, $excluded_categories );
add_filter( 'get_next_post_where', 'sql_get_next_post_where', $in_same_cat, $excluded_categories );

// print links
adjacent_post_link('%link', '%title'); // preious page
adjacent_post_link('%link', '%title', false, '', false); // next page

Works for me with subpages of the main page. It could be wrapped in a single template function but I find that a bit overkill. Let me know if you manage to apply it.

jevusi comments:

Ivaylo, you use get_previous_post_where but here i can see "Warning! This hook has been renamed!"
Is is not a problem ?

Ivaylo Draganov comments:

Yes, it is now a variable hook:
[[LINK href="$adjacent%7D_post_where"]]$adjacent%7D_post_where[[/LINK]]


It refers both to <em>get_next_post_where</em> and <em>get_previous_post_where</em>

I will try to see whether I can optimize that code down to a single function for applying the filters. But if you like, you can use it like that as well.

Ivaylo Draganov comments:

Seems like filters can't be applied inside of the same function, so you should use the code like it is. Just change the filters to avoid a PHP Notice for undefined variables:

add_filter( 'get_previous_post_where', 'sql_get_previous_post_where', false, '' );
add_filter( 'get_next_post_where', 'sql_get_next_post_where', false, '' );

I hope this is the result that you were after. Cheers :-)

Ivaylo Draganov comments:

You can change the way that links appear with by passing parameters to the function [[LINK href=""]]adjacent_post_link[[/LINK]]. There's no codex page for it, but the parameters are the same as for [[LINK href=""]]next_post_link[[/LINK]].

For example this gives you a link with the page title as its text:

adjacent_post_link('%link', '%title');

And this will just read "Previous page":

adjacent_post_link('%link', 'Previous page');

Appending/prepending text is also possible:

adjacent_post_link('Previous page: %link', '%title');


Ryan Riatno answers:

Try to use get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; in your page.php

Ryan Riatno comments:

<?php $temp_query = $wp_query; ?>
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
'category_name' => 'Tricks',
'post__not_in' => $sticky,
$my_query = new WP_Query($args);

while ($my_query->have_posts()) : $my_query->the_post();

jevusi comments:

is it a response to my question ?

Ryan Riatno comments:

You just use <!--nextpage--> in post editor

Ryan Riatno comments:

Oh sorry, I think I misunderstood your questions. Will try to solve the problem

Ryan Riatno comments:

Is this what you [[LINK href=""]]mean [[/LINK]] ?

jevusi comments:


Ryan Riatno comments:

Maybe this [[LINK href=""]]plugin [[/LINK]]would help you


Fahad Murtaza answers:

Are you still looking for an answer? If so , I can provide.