Question

I have my permalink set to %postname%/%location%/%course% I want this permalink %location%/%course%/%postname% but due to this I get 404 on every page but not on posts, I decided to put this permalink %postname%/%location%/%course%/%postname% but I need to strip the starting postname, in such a way I ll get no 404 pages and google will also index my site with this permalink /%location%/%course%/%postname% without any 404

how to do this????

This is my code, for displaying custom taxonomies on permalinks

add_filter('post_link', 'location_permalink', 10, 3);
add_filter('post_type_link', 'location_permalink', 10, 3);


    function location_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, '%location%') === FALSE) return $permalink;

            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;

            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'location');    
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'location';

        return str_replace('%location%', $taxonomy_slug, $permalink);
    }
Was it helpful?

Solution

Apologies that this doesn't directly answer your question but If I were you I would use the following structure:

/%post_id%/%location%/%course%/%postname%

Using the id should help to speed up the page load as it reduces the amount of processing that WP needs to do to select the correct page and you still get a nice enough url.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top