This quick post is about the issue when in WooCommerce website your WP short links (aka WordPress Plain URL) returns 404 error.
f.e. yourwebsite.com?p=912 should open (redirected to user friendly URL) the page of single post, where 912 is the ID of that post.
But in some WooCommerce websites due to complex permalinks rules, this redirection doesn’t work.
The reason is simple – The route requires the value for post_type – in order to determine what kind of post type is that $_GET[“p”] is.
So, let’s just help the route to find it:
add_action('init',function(){
if(isset($_GET["p"]) and !isset($_GET["post_type"])){
global $wpdb;
$post_type=$wpdb->get_var($wpdb->prepare("select post_type from $wpdb->posts where ID=%d",$_GET["p"]));
if($post_type)$_GET["post_type"]=$post_type;
}
},0);
This simple script above gets the value of post type from database, for the given post ID.
Or if it is needed just for products, not for other post types, then use faster one, without fetching info from DB.
add_action('init',function(){
if(isset($_GET["p"]) and !isset($_GET["post_type"])){
$_GET["post_type"]='product';
}
},0);
So, if you face a problem where you get 404 not found for plain WordPress links, then just implement one of two code blocks above. You can put
Discover more from WP DEV - Elvin Haci
Subscribe to get the latest posts sent to your email.