How to change the title URL of a specific post in WordPress?

Sometime, you might want to change the title url for a specific post in WordPress (for example to redirect to google.com instead of the post content page.) There are several ways to do this. The simplest way is to use a hook provided by WordPress. You can use the filter post_link.

You can check the details on this filter by visiting this WordPress Codex page. You can understand how to use the filter “post_link” with the following example found on this WordPress Codex page:

function append_query_string( $url, $post, $leavename ) {
if ( $post->post_type == 'post' ) {
$url = add_query_arg( 'foo', 'bar', $url );
}
return $url;
}
add_filter( 'post_link', 'append_query_string', 10, 3 );

To change the title URL of a specific post in WordPress, you need to change $url if the post id the one you want to redirect.  You can easily check the post id by using the “Inspect Element” feature in the browser such as Chrome.

To access the DevTools, please perform the following on any web page or app in Google Chrome:

  1. Open the Chrome menu at the top-right of your browser window, and then select Tools > Developer Tools.
  2. Right-click on any page element and select Inspect Element.

Post Id in WordPress

 

Or, you can use a plugin such as WPsite Show IDs. In this example, the post is “10762”.

Please add the following code to your theme function file:

// Change Post title url for a specific post in WordPress
function change_url_for_specific_post( $url, $post, $leavename ) {
$post_id = $post->ID;
if ( $post_id == '8790' ) { // Post ID 
$url = 'http://www.google.com'; // Please change this URL as you wish.
}
return $url;
}
add_filter( 'post_link', 'change_url_for_specific_post', 10, 3 );

Please change the post id as you wish. Now, if you click on the post title link whose post id is “8790”, it directs you to the url you designated (such as “www.google.com”.)

Change title url of a specific post in WordPress

When you hover your mouse over the post title, you can see the url for this post has been changed to “www.google.com” on the left bottom of your browser as shown. If you want to open the post in a separate tab or window, you can use the following jQuery script:

$(document).ready(function() {
$("class name").attr({"target" : "_blank"})
})

OR

$(document).ready(function(){
$('.class_name').click(function(){
window.open(this.href);
return false;
});
});

You might also use a redirection plugin for this purpose. There are several free and paid redirection tools for WordPress.

Please feel free to leave a Comment (). Have a great day.