Blogging

How to Redirect if Category or Tag Contains Only One Post WordPress

I was doing some research on wordpress tags and categories and the below script which is needed for redirecting single posts related categoies and tags to be redirected to the post itself in wordpress. Below code is wordpress redirect without plugin needed. it does not add more overhead on server performance.

Below code can be added to child theme(recommended) or to your theme’s functions.php file

function redirect_to_post(){
global $wp_query;
if( is_archive() && $wp_query->post_count == 1 ){
the_post();
$post_url = get_permalink();
wp_redirect( $post_url );
}

} add_action(‘template_redirect’, ‘redirect_to_post’);

Note: Child theme’s are safe in WordPress to be sure not to break anything all at once when doing customization.

Above can also used to redirect all posts of category to any single page etc… its just needs to be changed accordingly at $post_url line. Please let me know if you need more help on this.

If you need more of these snippets like below for customization then comment and i will post shortly but comment if needed urgently.

  1. wordpress redirect page to external url,
  2. wordpress redirect hook, wordpress redirect plugin
  3. redirect wordpress site to another url,
  4. wordpress 301 redirect,
  5. wordpress 301 redirect enabled,
  6. wordpress page template redirect,
  7. wordpress redirect referrer,
  8. wordpress redirect to new domain
  9. wordpress redirect to another page
  10. wordpress redirect http to https
  11. wordpress redirect url
  12. wordpress redirect after login
  13. wordpress redirect to https
  14. wordpress redirect after login based on role
  15. wordpress redirect all pages to one page
  16. wordpress redirect admin page
  17. wordpress redirect after submit comment
  18. wordpress redirect all pages to homepage
  19. wordpress redirect a page
Vamshi B

Recent Posts

How to install Firefox browser using Powershell

How to install of FireFox browser using PowerShell Sometimes you would like to install Firefox…

2 years ago

How to install chrome with Powershell?

Quick way to install chrome on windows with Powershell? Launch the Powershell and run below…

2 years ago

How to Fix PowerShell Script Not Digitally Signed Error?

How to Fix PowerShell Script Not Digitally Signed Error? When a script with a .ps1…

2 years ago

Powershell Cheat Sheet for beginners

Powershell Cheat Sheet for beginners PowerShell has become something of an ace in the hole…

2 years ago

Can you use Linux commands in Powershell?

Today we discuss one of a few questions a lot of sysadmins and IT Admins…

2 years ago

Backing Up an SQL Database with PowerShell

Backing Up an SQL Database with PowerShell Before making any changes in the production environment,…

2 years ago

This website uses cookies.