• I am trying to get my permalinks to be in this format for my custom post type

    http://localhost/trade/trade-category/post-name/post-id

    trade = custom post type
    trade-category = custom taxonomy
    post-name = title(slug) of the post
    post-id = id of the post

    With the code I have I get this permalink format

    http://localhost/trade/trade-category/post-id/post-name

    I want the post-id on the end of the permalink.

    Here is my code

    function tadl_create_trade_posttype() {
    
    	register_post_type( 'trade',
    		array(
    			'labels' => array(
    				'name' => __( 'Trades' ),
    				'singular_name' => __( 'Trade' )
    			),
    			'public' => true,
    			'has_archive' => true,
    			'rewrite'		=> array('slug' => 'trade/%trade-category%/%post_id%','with_front' => false),
    			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'comments' )
    		)
    	);
    }
    
    add_action( 'init', 'tadl_create_trade_posttype' );
    
    function tadl_taxonomies_trade() {
      $labels = array(
        'name'              => _x( 'Trade Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Trade Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Trade Categories' ),
        'all_items'         => __( 'All Trade Categories' ),
        'parent_item'       => __( 'Parent Trade Category' ),
        'parent_item_colon' => __( 'Parent Trade Category:' ),
        'edit_item'         => __( 'Edit Trade Category' ),
        'update_item'       => __( 'Update Trade Category' ),
        'add_new_item'      => __( 'Add New Trade Category' ),
        'new_item_name'     => __( 'New Trade Category' ),
        'menu_name'         => __( 'Trade Categories' ),
      );
    
      $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    	'rewrite' => array('slug' => 'trade'),
    	'_builtin'		=> false,
      );
    
      register_taxonomy( 'trade-category', 'trade', $args );
    }
    
    add_action( 'init', 'tadl_taxonomies_trade', 0 );
    
    add_filter('post_link', 'trade_permalink', 1, 3);
    add_filter('post_type_link', 'trade_permalink', 1, 3);
    
    function trade_permalink($permalink, $post_id, $leavename) {
    	global $wp_rewrite;
    
    	if(get_post_type() == 'trade') {
    		write_log($permalink);
    		if (strpos($permalink, '%trade-category%') === FALSE)
    			return $permalink;
    
    		$post = get_post($post_id);
    
    		if (!$post)
    			return $permalink;
    
    		$terms = wp_get_object_terms($post->ID, 'trade-category');
    
    		if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
    			$taxonomy_slug = $terms[0]->slug;
    		else
    			$taxonomy_slug = 'no-trade-category';
    
    		$permalink = str_replace('%trade-category%', $taxonomy_slug, $permalink);
    
    		//$permalink = str_replace('%postname%', $post->post_name, $permalink);
    
    		if ( $wp_rewrite->use_trailing_slashes )
    			return trailingslashit(str_replace('%post_id%', $post->ID, $permalink));
    		else
    			return untrailingslashit(str_replace('%post_id%', $post->ID, $permalink));
    
    	}
    
    	return $permalink;
    }

The topic ‘Permalinks for Custom Post’ is closed to new replies.