Using WordPress ‘get_previous_post_link()’ PHP function

The get_previous_post_link() WordPress PHP function retrieves the previous post link that is adjacent to the current post.

Usage

echo get_previous_post_link($format, $link, $in_same_term, $excluded_terms, $taxonomy);

Parameters

  • $format (string) Optional: Link anchor format. Default ‘« %link’.
  • $link (string) Optional: Link permalink format. Default ‘%title’.
  • $in_same_term (bool) Optional: Whether link should be in the same taxonomy term. Default: false.
  • $excluded_terms (int|string) Optional: Array or comma-separated list of excluded term IDs. Default: ”.
  • $taxonomy (string) Optional: Taxonomy, if $in_same_term is true. Default ‘category’.

More information

See WordPress Developer Resources: get_previous_post_link

Examples

Basic Usage

Display the previous post link with the default settings.

echo get_previous_post_link();

Custom Anchor Format

Display the previous post link with a custom anchor format.

echo get_previous_post_link('Previous: %link');

Display the previous post link with a custom permalink format.

echo get_previous_post_link('« %link', 'Go to %title');

Same Taxonomy Term

Display the previous post link only if it is in the same taxonomy term as the current post.

echo get_previous_post_link('« %link', '%title', true);

Exclude Terms and Custom Taxonomy

Display the previous post link, excluding specified term IDs and using a custom taxonomy.

echo get_previous_post_link('« %link', '%title', true, '5,7,9', 'custom-taxonomy');