Function and usage of get_adjacent_post_link() in wordpress


Answers ( 1 )


    The get_adjacent_post_link() function in WordPress is designed to retrieve the link for the adjacent post, which can be either the next or the previous post in relation to the current one. This function is particularly useful for creating navigation links within your WordPress theme.


    • Name: get_adjacent_post_link()


    • The function fetches the URL of the adjacent (next or previous) post. It's a versatile function that allows customization based on taxonomy terms, exclusion of specific terms, and direction (next or previous post).


    1. $format (string, required): Specifies the format for the link anchor. It's the text that will be displayed for the link.
    2. $link (string, required): Defines the permalink format for the link. This is essentially the URL structure of the adjacent post's link.
    3. $in_same_term (bool, optional): Determines whether the adjacent post link should be within the same taxonomy term as the current post. The default value is false.
    4. $excluded_terms (int[]|string, optional): An array or a comma-separated list of term IDs to exclude. If specified, posts within these terms will not be considered as adjacent. The default value is an empty string ''.
    5. $previous (bool, optional): Specifies whether to retrieve the link for the previous (true) or next (false) post. The default is true, which means it fetches the previous post by default.
    6. $taxonomy (string, optional): The taxonomy to consider when $in_same_term is true. The default taxonomy is 'category'.


    • Type: string
    • Description: Returns the URL of the adjacent (previous or next) post in relation to the current post.

    Sample Usage:

    // Example to get the previous post link in the same category
    $previous_post_link = get_adjacent_post_link('%link', 'Previous Post: %title', true, '', true, 'category');
    // Example to get the next post link, excluding certain term IDs
    $next_post_link = get_adjacent_post_link('%link', 'Next Post: %title', false, '3,7', false);
    // Displaying the links
    if($previous_post_link) {
        echo $previous_post_link;
    if($next_post_link) {
        echo $next_post_link;

    In these examples, %link is a placeholder for the anchor tag and %title is a placeholder for the post title. The function allows for flexibility in determining the navigation between posts based on various criteria like taxonomy terms, exclusions, and direction (previous/next).

Leave an answer