Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Use only for specific CPT posts
- */
- define('PM_CPT_NAME', 'courses');
- /**
- * The function that detects the CPT posts permalinks
- */
- function pm_detect_cpt_slugs($query) {
- global $wpdb, $pm_query, $wp, $wp_rewrite;
- // Do not run when Elementor is opened
- if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
- return $query;
- }
- // 1. Get the slug
- preg_match("/([^\/]+)(?:\/{$wp_rewrite->pagination_base}\/([\d]+))?$/", $wp->request, $parts);
- if(!empty($parts[1])) {
- $post_name = basename($parts[1]);
- $post_type = PM_CPT_NAME;
- // 2. Check if the slug is assigned to any post item
- $sql_query = "SELECT ID FROM $wpdb->posts WHERE 1=1 AND post_name = %s AND post_type = %s";
- $post_id = $wpdb->get_var($wpdb->prepare($sql_query, array($post_name, $post_type)));
- // 3. Filter the query if post was found
- if(!empty($post_id)) {
- $post = get_post($post_id);
- if(empty($post->post_type)) { return $query; }
- $post_type_object = get_post_type_object($post->post_type);
- $final_uri = (!empty($post->post_name)) ? $post->post_name : false;
- // Fix for hierarchical CPT & pages
- if(!(empty($post->ancestors)) && !empty($post_type_object->hierarchical)) {
- foreach($post->ancestors as $parent) {
- $parent = get_post($parent);
- if($parent && $parent->post_name) {
- $final_uri = $parent->post_name . '/' . $final_uri;
- }
- }
- }
- $new_query = array(
- 'post_type' => $post->post_type,
- $post->post_type => $final_uri,
- 'name' => $final_uri,
- '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);
- $query = $new_query;
- }
- }
- if(isset($_GET['debug_query'])) {
- echo sprintf('<pre>Old query: %s</pre>', print_r($query, true));
- echo (!empty($new_query)) ? sprintf('<pre>New query: %s</pre>', print_r($new_query, true)) : '';
- echo (!empty($parts)) ? sprintf('<pre>Parts: %s</pre>', print_r($parts, true)) : '';
- die();
- }
- return $query;
- }
- add_filter('request', 'pm_detect_cpt_slugs', 9999);
- function bis_filter_cpt_permalinks($url, $element) {
- if(!empty($element->post_type) && ($element->post_type == PM_CPT_NAME)) {
- $full_uri = get_page_uri($element->ID);
- $url = trim(get_option('home'), '/') . "/{$full_uri}";
- }
- return $url;
- }
- add_filter('post_type_link', 'bis_filter_cpt_permalinks', 999, 2);
- function pm_redirect_native_cpt_permalinks() {
- global $wp;
- if(!empty($wp->request) && is_singular(PM_CPT_NAME)) {
- $cpt_data = get_post_type_object(PM_CPT_NAME);
- $cpt_slug = preg_quote($cpt_data->rewrite['slug'], '/');
- if(!empty($cpt_slug) && preg_match("/^({$cpt_slug})/", $wp->request)) {
- $url = get_permalink();
- wp_safe_redirect($url, 301);
- exit();
- }
- }
- }
- add_action('template_redirect', 'pm_redirect_native_cpt_permalinks', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement