get_previous_post

Home » Functions » get_previous_post

The WordPress function get_previous_post is used to retrieve the previous post relative to the current post within the same post type. It allows you to navigate to the previous post based on the post’s publish date, allowing users to easily navigate through the content.

This function takes in several optional parameters such as the post type, taxonomy, and the order in which the posts should be retrieved. By default, it will return the previous post based on the publish date of the current post.

Here’s an example usage code:

$previous_post = get_previous_post();

if ( $previous_post ) {
    echo '<a href="' . get_permalink( $previous_post->ID ) . '">Previous Post: ' . get_the_title( $previous_post->ID ) . '</a>';
}

In this example, we first call the get_previous_post function to get the previous post object. We then check if there is a previous post available. If there is, we display a link to it using get_permalink to get the URL and get_the_title to get the title of the previous post.

By utilizing the get_previous_post function, you can enhance the navigation experience on your WordPress website, allowing users to easily explore the content in a logical order.

Learn More on WordPress.org

WordPress snippets using the get_previous_post function

Register an account to save your snippets or go Pro to get more features.