A Universal Method That Works for Any Content Type
Websites, WordPress customization

How to Redirect WordPress Pages and WooCommerce Products Using Post IDs

A Universal Method That Works for Any Content Type

Learn how to create bulletproof 301 redirects in WordPress for pages, posts, and WooCommerce products using a single universal function. Perfect for site migrations and URL changes.

When managing a WordPress website, especially one with WooCommerce, you’ll often need to redirect old URLs to new ones. Whether you’re consolidating content, fixing broken links, or restructuring your site, proper redirects are crucial for maintaining SEO value and user experience.

Most tutorials focus on using WordPress conditional functions like is_page() or is_single(), but there’s a more elegant and universal approach that works consistently across all content types.

The Problem with Traditional Redirect Methods

The typical approach uses WordPress conditional functions: 

				
					// Traditional approach - can be unreliable
if (is_page(123)) {
    wp_redirect(get_permalink(456), 301);
    exit;
}
				
			

This method has several limitations:

  • is_page() only works for static pages, not for WooCommerce products
  • Different content types require different conditional functions
  • Can fail if the query isn’t fully loaded when the function runs
  • Becomes messy when handling multiple content types

The Universal Solution: Using Post IDs

Instead of relying on content-type-specific functions, we can use a universal approach that works with any WordPress content type:

				
					function universal_page_redirects() {
    // Get the current post ID
    $current_post_id = get_the_ID();
    
    // Define your redirects (from => to)
    $redirects = array(
        6922 => 3800,  // Product ID 6922 to Page ID 3800
        421  => 3800,  // Page ID 421 to Page ID 3800
        // Add more redirects as needed
    );
    
    // Check if current post needs to be redirected
    if (array_key_exists($current_post_id, $redirects)) {
        $redirect_url = get_permalink($redirects[$current_post_id]);
        
        // Prevent redirect loops and ensure URL exists
        if ($redirect_url && $redirect_url != get_permalink($current_post_id)) {
            wp_redirect($redirect_url, 301);
            exit;
        }
    }
}
add_action('template_redirect', 'universal_page_redirects'); 
				
			

Why This Method Works Better

1. Content Type Agnostic

get_the_ID() works for:

  • WordPress pages
  • Blog posts
  • WooCommerce products
  • Custom post types
  • Any content with a post ID

2. Loop Prevention

The condition $redirect_url != get_permalink($current_post_id) prevents infinite redirect loops.

3. Error Handling

The check if ($redirect_url) ensures the destination URL exists before redirecting.

4. Scalable

Easy to add multiple redirects by expanding the array.

Simplified Version for Quick Implementation

If you only need a few redirects, here’s a streamlined version:

				
					function simple_page_redirects() {
    $current_post_id = get_the_ID();
    
    // Redirect specific posts to target page
    if ($current_post_id == 6922 || $current_post_id == 421) {
        $redirect_url = get_permalink(3800);
        if ($redirect_url && $redirect_url != get_permalink($current_post_id)) {
            wp_redirect($redirect_url, 301);
            exit;
        }
    }
}
add_action('template_redirect', 'simple_page_redirects');
				
			

Advanced: Bulk Redirects with URL Mapping

For complex migrations, you can create a more sophisticated system:
				
					function advanced_redirect_system() {
    $current_post_id = get_the_ID();
    
    // Complex redirect mappings
    $redirect_rules = array(
        // Redirect old products to new categories
        'old_products' => array(
            'ids' => array(100, 101, 102, 103),
            'target' => 2000
        ),
        // Redirect discontinued pages to homepage
        'discontinued' => array(
            'ids' => array(200, 201, 202),
            'target' => get_option('page_on_front') // Homepage
        )
    );
    
    foreach ($redirect_rules as $rule) {
        if (in_array($current_post_id, $rule['ids'])) {
            $redirect_url = get_permalink($rule['target']);
            if ($redirect_url && $redirect_url != get_permalink($current_post_id)) {
                wp_redirect($redirect_url, 301);
                exit;
            }
            break; // Stop after first match
        }
    }
}
add_action('template_redirect', 'advanced_redirect_system');
				
			

Implementation Steps

  1. Add to functions.php: Place your chosen code in your theme’s functions.php file
  2. Find Post IDs: Use wp-admin to find the IDs of posts you want to redirect (visible in the URL when editing)
  3. Test thoroughly: Always test redirects to ensure they work correctly
  4. Monitor: Check that redirects don’t create loops or break functionality

Best Practices

  • Use 301 redirects for permanent moves to maintain SEO value
  • Test redirects before deploying to production
  • Document your redirects for future reference
  • Monitor for redirect loops using tools like Screaming Frog
  • Consider using a plugin for complex redirect management

Troubleshooting

If redirects aren’t working:

  1. Check post IDs are correct
  2. Verify target URLs exist and are accessible
  3. Clear caching plugins and browser cache
  4. Test in incognito mode to avoid cached results

Conclusion

This universal redirect method provides a robust, scalable solution for managing WordPress redirects across all content types. By using post IDs instead of content-type-specific functions, you create more reliable redirects that work consistently regardless of your site structure.

The approach is particularly valuable for WooCommerce sites where products, pages, and posts need to be handled uniformly, making site maintenance much more straightforward.

Need help with this solution or looking for custom development?

Visit my Stay In Touch page to connect and discuss your project. Discover a range of web development services and specialized WordPress solutions tailored to your needs. Let's work together to enhance your digital presence.