Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Detect posts
- */
- function pm_detect_post_slugs($query) {
- global $wpdb, $pm_query;
- // Do not run if custom permalink was detected
- if(!empty($pm_query['id'])) {
- return $query;
- }
- // 1. Get the slug
- if(!empty($query['pagename'])) {
- $post_name = $query['pagename'];
- } else if(!empty($query['name'])) {
- $post_name = $query['name'];
- } else if(!empty($query['attachment'])) {
- $post_name = $query['attachment'];
- }
- if(!empty($post_name)) {
- $post_name = basename($post_name);
- // 2. Check if the slug is assigned to any post item
- $sql_query = "SELECT post_name, post_type FROM $wpdb->posts WHERE 1=1 AND post_name = %s";
- $post = $wpdb->get_row($wpdb->prepare($sql_query, array($post_name)));
- // 3. Filter the query if post was found
- if(!empty($post->post_name)) {
- $page = (!empty($query['page'])) ? $query['page'] : 1;
- $new_query = array(
- 'post_type' => $post->post_type,
- $post->post_type => $post->post_name,
- 'page' => $page,
- 'do_not_redirect' => 1,
- );
- // 4. Disable canonical redirect
- remove_action('template_redirect', 'wp_old_slug_redirect');
- remove_action('template_redirect', 'redirect_canonical');
- add_filter('wpml_is_redirected', '__return_false', 99, 2);
- add_filter('pll_check_canonical_url', '__return_false', 99, 2);
- return $new_query;
- }
- }
- return $query;
- }
- add_filter('request', 'pm_detect_post_slugs', 9999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement