• Resolved dnxpert

    (@dnxpert)


    Hi guys,

    I have a really weird issue with custom post type custom permalinks on the admin side that i have Googled and searched on the forums without a solution for days.

    I have two custom post type definitions:

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'logo',
        array(
          'labels' => array(
            'name' => __( 'Logos' ),
            'singular_name' => __( 'Logo' )
          ),
          'public' => true,
          'rewrite' => false,
    	  'hierarchical' => true,
    	  'supports' => array('title','author','editor','comments', 'custom-fields', 'revisions'),
    	  'rewrite' => array('slug' => 'logo/%logo_id%', 'with_front' => 'false')
        )
      ); 
    
      register_post_type( 'template',
        array(
          'labels' => array(
            'name' => __( 'Templates' ),
            'singular_name' => __( 'Template' )
          ),
          'public' => true,
          'rewrite' => false,
    	  'hierarchical' => true,
    	  'supports' => array('title','author','editor','comments', 'custom-fields', 'revisions'),
    	  'rewrite' => array('slug' => 'template/%template_id%', 'with_front' => 'false')
        )
      );
    }

    I have the following custom rewrite rules:

    add_action('init', 'custom_rewrite');
    function custom_rewrite() {
    	global $wp_rewrite;
    	$wp_rewrite->add_rewrite_tag("%logo_id%", '([^/]+)', "post_type=logo&p=");
    	$wp_rewrite->add_permastruct('logo', "logo/%logo_id%", false);
    	$wp_rewrite->add_rewrite_tag("%template_id%", '([^/]+)', "post_type=template&p=");
    	$wp_rewrite->add_permastruct('template', "template/%template_id%", false);
    }
    
    add_filter('post_type_link', 'custom_logo_permalink', 1, 3);
    function custom_logo_permalink($post_link, $id = 0, $leavename) {
    	return custom_post_permalink($id, 'logo');
    }
    
    add_filter('post_type_link', 'custom_template_permalink', 1, 3);
    function custom_template_permalink($post_link, $id = 0, $leavename) {
    	return custom_post_permalink($id, 'template');
    }
    
    function custom_post_permalink($id, $type) {
    	global $wp_rewrite;
    	$post = &get_post($id);
    	if ( is_wp_error( $post ) )
    		return $post;
    	$newlink = $wp_rewrite->get_extra_permastruct($type);
    
    	switch ($type) {
    		case 'logo' : $newlink = str_replace("%logo_id%", $post->ID, $newlink); break;
    		case 'template' : $newlink = str_replace("%template_id%", $post->ID, $newlink); break;
    		default :  $newlink = str_replace("%post_id%", $post->ID, $newlink); break;
    	}
    
    	$newlink = home_url(user_trailingslashit($newlink));
    	return $newlink;
    }

    Now, my links work fine on the front end.

    mydomain.com/logo/12 gives me the correct logo post type entry
    and mydomain.com/template/34 gives me the correct template post type entry.

    However, on the admin side, under logo custom post types list view, when I hover over the View button I see mydomain/template/12 while under the template admin list view I see mydomain/template/34. It appears in both instances wordpress is using the latest custom permalink slug to create the preview “View” link.

    I am stumped as to why the admin side does not use the correct permalink structure when creating the preview links.

    Any help would be thoroughly appreciated.

    dnx

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

    (@dnxpert)

    Any replies please?

    Thread Starter dnxpert

    (@dnxpert)

    Never mind, I had caused the issue myself. I was adding duplicate filters as can be seen above in code for custom_template_permalink and custom_logo_permalink when in fact I should have added only 1 and used the retrieved $post->post_type to differentiate between the post types.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Weird permalinks on admin side’ is closed to new replies.