Skip to content

Instantly share code, notes, and snippets.

@sungraiz
Created May 13, 2020 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sungraiz/330c20a8f847be9136b65cf2e7b8f99f to your computer and use it in GitHub Desktop.
Save sungraiz/330c20a8f847be9136b65cf2e7b8f99f to your computer and use it in GitHub Desktop.
Replace Property Slug With Taxonomy
<?php
add_filter('post_link', 'inspiry_post_type_permalink', 20, 3);
add_filter('post_type_link', 'inspiry_post_type_permalink', 20, 3);
function inspiry_post_type_permalink($permalink, $post_id, $leavename) {
$post_type_name = 'property'; // post type name, you can find it in admin area or in register_post_type() function
$post_type_slug = 'property'; // the part of your product URLs, not always matches with the post type name
$tax_name = 'property-status'; // the product categories taxonomy name
$post = get_post( $post_id );
if ( strpos( $permalink, $post_type_slug ) === FALSE || $post->post_type != $post_type_name ) // do not make changes if the post has different type or its URL doesn't contain the given post type slug
return $permalink;
$terms = wp_get_object_terms( $post->ID, $tax_name ); // get all terms (product categories) of this post (product)
if ( !is_wp_error( $terms ) && !empty( $terms ) && is_object( $terms[0] ) ) // rewrite only if this product has categories
$permalink = str_replace( $post_type_slug, $terms[0]->slug, $permalink );
return $permalink;
}
add_filter('request', 'inspiry_post_type_request', 1, 1 );
function inspiry_post_type_request( $query ){
global $wpdb;
$post_type_name = 'property'; // specify your own here
$tax_name = 'property-status'; // and here
$slug = $query['attachment']; // when we change the post type link, WordPress thinks that these are attachment pages
// get the post with the given type and slug from the database
$post_id = $wpdb->get_var(
"
SELECT ID
FROM $wpdb->posts
WHERE post_name = '$slug'
AND post_type = '$post_type_name'
"
);
$terms = wp_get_object_terms( $post_id, $tax_name ); // our post should have the terms
if( isset( $slug ) && $post_id && !is_wp_error( $terms ) && !empty( $terms ) ) : // change the query
unset( $query['attachment'] );
$query[$post_type_name] = $slug;
$query['post_type'] = $post_type_name;
$query['name'] = $slug;
endif;
return $query;
}
add_action('template_redirect', 'inspiry_post_type_redirect');
function inspiry_post_type_redirect() {
$post_type_name = 'property'; // specify your own here
$post_type_slug = 'property'; // here
$tax_name = 'property-status'; // and here
if( strpos( $_SERVER['REQUEST_URI'], $post_type_slug ) === FALSE) // do not redirect if the URL doesn't contain the given post type slug
return;
if( is_singular( $post_type_name ) ) : // if post type page
global $post, $wp_rewrite;
$terms = wp_get_object_terms( $post->ID, $tax_name ); // get terms attached
if ( !is_wp_error( $terms ) && !empty( $terms ) && is_object( $terms[0] ) ) :
wp_redirect( site_url() . '/' . $wp_rewrite->front . '/' . $terms[0]->slug . '/' . $post->post_name, 301 );
// wp_redirect( get_permalink( $post->ID ), 301 ); // depends on the previous code from this post
exit();
endif;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment