Add Rel Next and Prev Tags to WordPress Archive Pages

WordPress generates archive pages for all your category, tag and author pages. In addition there are date based archives that archive posts based on the month, year and day. From the SEO perspective, all these pages look very similar to each other considering that they have very little content and share the same title tags except the minor change in the page number. Because of this they could end up being identified as duplicate content or thin pages by search engines.

One simple way to avoid this from happening is to add the rel="next" and rel="prev" to these pages as advised by the Google webmaster team in this blog post.

Example of how the tags will appear:

If you are on page 3 of a paginated category archive, the tags would appear as follows:

<link rel="next" href="" />
<link rel="prev" href="" />

If you are on page 2, the tags would appear as follows:

<link rel="next" href="" />
<link rel="prev" href="" />

Notice that the prev tag points to the main post and not to page/1/. This is because wordpress reserves page/1/ for the main page. In other words, Page/1/ would redirect to the main/first page of the paginated archive.

The presence of these tags helps Google and other search engines understand that the paginated archive pages are part of a sequence and that they should be considered as a single page instead of separate pages. So this means, you could have a category archive page that runs into many paginated pages but it will still be considered a single page. This is really great from the search engine optimization perspective making this a must have tag especially if your blog has many paginated archive pages. So how do you add them?

Now the point to note is that wordpress already adds these tags, but only on non-paginated single post pages. They do not appear on archive pages or paginated single post pages as they should. But no worries, you can add these tags to all your archive pages using a plugin like the Yoast SEO plugin or using a simple function as explained below.

Function to Add Next and Prev Tags to Paginated Archives

The following function adds these tags to all the standard wordpress archive pages which include the following:

  • Homepage (If your homepage is paginated)
  • Category and Tag archives.
  • Author archives.
  • Date based archives - Month, Year and Day based archives.

Open your theme's functions.php page and add the following function towards the end of the page.

/*Function to add Next and Prev tags to Paginated Homepage and Archive Pages*/
function add_rel_prev_next_archive_pages(){

if( is_archive() || is_home() || is_front_page() ){
global $wp_rewrite, $wp_query;

#### 1: Get pagination base. ####
$pagination_base = sanitize_text_field(trailingslashit($wp_rewrite->pagination_base));

#### 2: Get page number. ####
$paged = intval( get_query_var( 'paged' ) );
if($paged == 0){ $paged = 1;}

#### 3: Get Permalinks ####
# Get permalink for homepage.
if( is_home() || is_front_page() ){
$perm_link = get_option('siteurl');
# Get permalink for category or tag archives.
if( is_category() || is_tag() ){
	$term_val = get_queried_object();
	$perm_link = get_term_link( $term_val->term_id, $term_val->taxonomy );
# Get permalink for author archives.
if( is_author() ){
	$perm_link = get_author_posts_url( get_query_var('author') );
# Get permalink for date archives.
	if( is_day() ){
	$perm_link =  get_day_link( get_post_time('Y'), get_post_time('m'), get_post_time('j') );
	}elseif( is_month() ){
	$perm_link =  get_month_link( get_post_time('Y'), get_post_time('m') );
	}elseif( is_year() ){
	$perm_link =  get_year_link( get_post_time('Y') );

#### 4: Add the tags if pagination is present ####
if( ! empty( $perm_link ) ){
# Add rel="next" tag.
if(true == get_next_posts_link()){
	$nxt_page_num = ($paged + 1);
	$full_url = user_trailingslashit(trailingslashit($perm_link) . $pagination_base . $nxt_page_num);
	echo '<link rel="next" href="' . esc_url($full_url) . '" />';
	echo "\n";}
# Add rel="prev" tag.	
if(true == get_previous_posts_link()){
	$prev_page_num = ($paged - 1);
	if($paged == 2){$prev_page_num = ''; $pagination_base = '';}
	$full_url = user_trailingslashit(trailingslashit($perm_link) . $pagination_base . $prev_page_num);
	echo '<link rel="prev" href="'. esc_url($full_url) . '" />';
	echo "\n";}
add_action('wp_head', 'add_rel_prev_next_archive_pages', 3);

Code Explanation

In wordpress, the paginated archive page URLs have the permalink followed by the pagination base and the page number. There is no direct way of retrieving this URL. The only way would be to construct this URL by separately getting the permalink, pagination base and page number and then concatenating them to form the full URL as shown in the image below:

Wordpress pagianted URL

# 1: Getting the pagination base
We start by getting the pagination base by accessing the pagination base property of the $wp_rewrite global variable.

global $wp_rewrite;

# 2: Getting the page number
We then get the current page number by accessing the $paged global variable. Here we use the get_query_var() function to get the value of this variable.

# 3: Getting Permalinks
For archive pages, we cannot use the get_permalink() function to get the permalinks as you do in case of the single post pages. The permalink needs to be generated using special functions and by accessing the global variables.

3.a) Getting Homepage Permalink: For homepage, we use the get_option('siteurl') function to get the link to the wordpress web address. This is the value that has been set in Settings > General > Site Address.

3.b) Getting Category and Tag Permalinks: The get_term_link() function can be used to get permalinks for taxonomies like the category pages. This function requires the category ID and the taxonomy name which can be retrieved using the get_queried_object() functio. Taxonomy is used to to identify if the current archive is a category or a tag archive.

3.c) Getting Author Archive Permalinks: The author page permalink can be retrieved using the get_author_posts_url() function. This function needs to be supplied with the author ID which we get using the get_query_var() function as follows:

get_author_posts_url( get_query_var('author') );

You can also get the author ID using the global $post object using $post->post_author.

global $post;
$perm_link = get_author_posts_url( $post->post_author );

3.d) Getting Date Archive Permalinks: There are three date archives that wordpress generates and these include the monthly, yearly and daily archives.

The wordpress functions get_day_link(), get_month_link() and get_year_link() can be used to retrieve the permalink for the daily, monthly and yearly archives. To make these functions return the proper permalink we need to supply the function with archived post's month, year and day which can be done using the functions get_post_time('Y'), to get the year, get_post_time('m'), to get the month and get_post_time('j'), to get the day when the archived post was published.

Daily Archive Link: get_day_link( get_post_time('Y'), get_post_time('m'), get_post_time('j') ); 
Monthly Archive Link: get_month_link( get_post_time('Y'), get_post_time('m') );
Yearly Archive Link: get_year_link( get_post_time('Y') );

# 4: Adding the Tags to Paginated Pages

The wordpress functions get_next_posts_link() and get_previous_posts_link() can be used to determine is the given archive page is paginated. Also, based on which page you are on, these functions can be used to check if there is a paginated page in the forward or backward sequence.

For example, if you are on page 5, you can use the get_next_posts_link() to check if there is a page 6. The function will return true is there is a Page 6 or false is there isn't.

So using these functions we can easily add the next and prev tags to all our archive pages by figuring out if or not a next or previous page exists.

Once a paginated page is determined, the function generates the full URL by concatenating the permalink, pagination base and the page number and then outputs link within the proper rel meta tag.

So this is how you can easily add these tags to your WordPress archive and paginated homepage pages without using a plugin. Please note that this function will only work if you have permalinks enabled in your account. Once you have added the function, make sure to check all paginated archives to ensure that the tags are being added properly. If you are using frameworks like the Genesis framework, you might have to tweak the function a little.

To see how you can add these tags to paginated single posts, check this article. It is s good practice to also add a nofollow tag to paginated archives, check out how here.



  1. Vincy says:

    Got the following simpler solution. Is this okay or do you find any issues in that?

    <link rel="prev" href="" />
    <link rel="next" href="" />

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.