Title: get_adjacent_post_rel_link
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_adjacent_post_rel_link( string $title, bool $in_same_term = false, int[]|string $excluded_terms, bool $previous = true, string $taxonomy ): string|void

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#wp--skip-link--target)

Retrieves the adjacent post relational link.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#description)󠁿

Can either be next or previous post relational link.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#parameters)󠁿

 `$title`stringoptional

Link title format. Default `'%title'`.

`$in_same_term`booloptional

Whether link should be in the same taxonomy term.

Default:`false`

`$excluded_terms`int[]|stringoptional

Array or comma-separated list of excluded term IDs.
 Default empty.

`$previous`booloptional

Whether to display link to previous or next post.

Default:`true`

`$taxonomy`stringoptional

Taxonomy, if `$in_same_term` is true. Default `'category'`.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#return)󠁿

 string|void The adjacent post relational link URL.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#source)󠁿

    ```php
    function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    	$post = get_post();
    	if ( $previous && is_attachment() && $post ) {
    		$post = get_post( $post->post_parent );
    	} else {
    		$post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
    	}

    	if ( empty( $post ) ) {
    		return;
    	}

    	$post_title = the_title_attribute(
    		array(
    			'echo' => false,
    			'post' => $post,
    		)
    	);

    	if ( empty( $post_title ) ) {
    		$post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
    	}

    	$date = mysql2date( get_option( 'date_format' ), $post->post_date );

    	$title = str_replace( '%title', $post_title, $title );
    	$title = str_replace( '%date', $date, $title );

    	$link  = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
    	$link .= esc_attr( $title );
    	$link .= "' href='" . get_permalink( $post ) . "' />\n";

    	$adjacent = $previous ? 'previous' : 'next';

    	/**
    	 * Filters the adjacent post relational link.
    	 *
    	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
    	 * of adjacency, 'next' or 'previous'.
    	 *
    	 * Possible hook names include:
    	 *
    	 *  - `next_post_rel_link`
    	 *  - `previous_post_rel_link`
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param string $link The relational link.
    	 */
    	return apply_filters( "{$adjacent}_post_rel_link", $link );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/link-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/link-template.php#L2062)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/link-template.php#L2062-L2112)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#hooks)󠁿

 [apply_filters( “{$adjacent}_post_rel_link”, string $link )](https://developer.wordpress.org/reference/hooks/adjacent_post_rel_link/)

Filters the adjacent post relational link.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#related)󠁿

| Uses | Description | 
| [is_attachment()](https://developer.wordpress.org/reference/functions/is_attachment/)`wp-includes/query.php` |

Determines whether the query is for an existing attachment page.

  | 
| [mysql2date()](https://developer.wordpress.org/reference/functions/mysql2date/)`wp-includes/functions.php` |

Converts given MySQL date string into a different format.

  | 
| [get_adjacent_post()](https://developer.wordpress.org/reference/functions/get_adjacent_post/)`wp-includes/link-template.php` |

Retrieves the adjacent post.

  | 
| [the_title_attribute()](https://developer.wordpress.org/reference/functions/the_title_attribute/)`wp-includes/post-template.php` |

Sanitizes the current title when retrieving or displaying.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [get_permalink()](https://developer.wordpress.org/reference/functions/get_permalink/)`wp-includes/link-template.php` |

Retrieves the full permalink for the current post or post ID.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 6 more](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#)

| Used by | Description | 
| [adjacent_posts_rel_link()](https://developer.wordpress.org/reference/functions/adjacent_posts_rel_link/)`wp-includes/link-template.php` |

Displays the relational links for the posts adjacent to the current post.

  | 
| [next_post_rel_link()](https://developer.wordpress.org/reference/functions/next_post_rel_link/)`wp-includes/link-template.php` |

Displays the relational link for the next post adjacent to the current post.

  | 
| [prev_post_rel_link()](https://developer.wordpress.org/reference/functions/prev_post_rel_link/)`wp-includes/link-template.php` |

Displays the relational link for the previous post adjacent to the current post.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.8.0](https://developer.wordpress.org/reference/since/2.8.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_adjacent_post_rel_link%2F)
before being able to contribute a note or feedback.