Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • kingcrunk

    (@kingcrunk)

    odr_m9611, the following code should work for you:

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

    Let me know if it works!

    kingcrunk

    (@kingcrunk)

    Actually, I am still getting some errors.. for instance the php get_permalink is pulling them like this:
    http://iloveomfg.com/tracks/377/%postname%/

    This totally makes no sense. I think it is because I messed with the code. Damn.

    kingcrunk

    (@kingcrunk)

    I’ve come up with a solution to this, finally!

    Here’s how I’m using it:

    Permalink structure =
    / Custom Post Type Slug / Post ID / Post Name Slug /
    aka
    http://iloveomfg.com/tracks/%post_id%/%postname%/
    which outputs
    http://iloveomfg.com/tracks/377/flux-pavilion-bass-cannon-flufftronix-nolibs-club-edit/

    —–
    The trick was to replace the string %cpt_entry% with the already built-in WP permalink string %postname%. Doing this eliminates the 404/Page Not Found errors altogether, and also allows you to edit the post slug in the post editor like you can normally.

    Here is the final code which I used, based on dpchen’s earlier post in this thread.

    Add the following to functions.php:

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

    The great thing about using this type of permalink structure, and the specific reason I am doing it is because it allows you to make easy short-links of your urls… for instance:
    http://iloveomfg.com/tracks/377/
    works the same as
    http://iloveomfg.com/tracks/377/flux-pavilion-bass-cannon-flufftronix-nolibs-club-edit/

    However, for some reason, this does not work:
    http://iloveomfg.com/tracks/flux-pavilion-bass-cannon-flufftronix-nolibs-club-edit/
    For me, it displays an archive of all my custom post type “tracks”.

    Hope that helps you guys out!

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