• Hey everyone,

    I have 3 custom post types setup and I have created a custom permalink structure for each. The problem I am having is that the third custom post type I have setup with the custom permalink, is being used for the previous 2.

    Example:

    • Classified
    • Employment
    • Sponsorship

    The above are the post types and I have the custom permalink structure set to (for each): /post_type/post_id/

    Now, The above order are the order they are set in and sponsorship takes over all of them. So for instance:

    • /classified/100/
    • /employment/101/
    • /sponsorship/102/

    The above all use the permalink:

    • /sponsorship/100/
    • /sponsorship/101/
    • /sponsorship/102/

    What am I doing wrong to get this error happening? Is it a permalink structure error? below is my code I am using to create the custom permalink structures for each. The only difference is that %c_id% is changed to %e_id% and %s_id% for classifieds, employment and sponsorship. And also any reference to ‘classifieds’ is changed to employment and sponsorship respectively.

    add_action('init', 'classifieds_rewrite');
    function classifieds_rewrite() {
        global $wp_rewrite;
    
        $queryarg = 'post_type=classifieds&p=';
        $wp_rewrite->add_rewrite_tag('%c_id%', '([^/]+)', $queryarg);
        $wp_rewrite->add_permastruct('classifieds', '/classifieds/%c_id%/', false);
    }
    
    add_filter('post_type_link', 'classifieds_permalink', 1, 3);
    function classifieds_permalink($post_link, $id = 0) {
        global $wp_rewrite;
        $post = &get_post($id);
        if ( is_wp_error( $post ) )
            return $post;
        $newlink = $wp_rewrite->get_extra_permastruct('classifieds');
        $newlink = str_replace("%c_id%", $post->ID, $newlink);
        $newlink = home_url(user_trailingslashit($newlink));
        return $newlink;
    }

    Also, it’s not that it is breaking, it works fine, goes to the pages, e.g. classifieds/101/ etc… But the get_permalink() function call and where it thinks it is, is: sponsorship/101/

    Thanks for the help! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Lee Wallis

    (@fatwombat)

    Anyone have any ideas at all? I’m willing to try anything ??

    Thread Starter Lee Wallis

    (@fatwombat)

    OK so the below code has now worked for my problem above. Hopefully this helps for someone else ??

    This is the option in the register_post_type

    'rewrite' => array('slug' => 'post_type/%post_id%', 'with_front' => false)

    And the code that worked for it

    add_filter('post_type_link', 'post_type_link', 1, 3);
    function post_type_link( $post_link, $post = 0, $leavename = false ) {
    
    	if ( strpos('%post_id%', $post_link) === 'false' ) {
    		return $post_link;
    	}
    	if ( is_object($post) ) {
    		$post_id = $post->ID;
    	} else {
    		$post_id = $post;
    		$post = get_post($post_id);
    	}
    	if ( !is_object($post) || $post->post_type != 'your_custom_post_type' ) {
    		return $post_link;
    	}
    
      	//put post ID in place of %post_id%
    	return str_replace('%post_id%', $post_id, $post_link);
    
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Post Type with Custom URLs’ is closed to new replies.