Last active
February 5, 2016 21:28
-
-
Save shitpoet/2a513ff55028c6925627 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// allow product links w/o `product/` base for wc | |
// additionaly you can add | |
// RedirectMatch 301 ^/product/(.*)$ /$1 | |
// to `.htaccess` to handle `page?foo=bar` queries | |
// this script is based on this article by Ryan Sechrest: | |
// http://ryansechrest.com/2013/04/remove-post-type-slug-in-custom-post-type-url-and-move-subpages-to-website-root-in-wordpress/ | |
add_action( | |
'pre_get_posts', | |
'custom_pre_get_posts' | |
); | |
function custom_pre_get_posts($query) { | |
global $wpdb; | |
if(!$query->is_main_query()) { | |
return; | |
} | |
$post_name = $query->get('name'); | |
$post_type = $wpdb->get_var( | |
$wpdb->prepare( | |
'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1', | |
$post_name | |
) | |
); | |
switch($post_type) { | |
case 'product': | |
//$query->set('product', $post_name); | |
//$query->set('post_type', $post_type); | |
$query->set('post_type', 'product'); | |
$query->is_single = true; | |
$query->is_page = false; | |
break; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment