WordPress.org

Ready to get started?Download WordPress

Forums

URL rewriting hack for custom post types (3 posts)

  1. jonathanroy
    Member
    Posted 3 years ago #

    Hello hello,

    I'm pretty much new to the new and awesome feature wordpress released for us it's latest version: custom post types.

    I managed to set them up in the admin, publish some and display them on my blog.

    The problem is that even after many hours of playing around and searching online, I can't find a way to rewrite my URLs in a way that makes sense.

    I have about 4 different custom post types and they all share the same categories and sub-categories. I would like the URLs to look like this:

    View all posts by post category and type: http://myblog.com/category/category/post_type/
    Paginated: http://myblog.com/category/category/post_type/page-2/

    View a specific post:
    http://myblog.com/category/category/post_type/post_id-postname/

    I have tried to play with the $wp_rewrite array, the create_rewrite_rules hook, the custom_permalink hook, etc. but I can't make anything actually work the way I want.

    I have actually been able to figure a work around to get this structure: http://myblog.com/post_type/category/category/

    But then the posts don't work when I go to: http://myblog.com/post_type/category/category/post_id-postname/

    Here is the latest code I have been playing with.

    [Large code excerpt removed by moderator per forum rules. Please use the pastebin for all large code excerpts. It works better anyway.]

  2. jonathanroy
    Member
    Posted 3 years ago #

    I am now using these rules:

    add_action('init', 'create_rewrite_rules');
    function create_rewrite_rules() {
    
      global $wp_rewrite;
    
      $custom_rules = array(
        'section' => '/%section%/%category%/',
        'news' => '/%section%/%category%/%year%/%monthnum%/%post_id%-%news%/',
        'jobs' => '/%section%/%category%/%post_id%-%jobs%/',
        'employers' => '/%section%/%category%/%post_id%-%employers%/'
      );
    
      foreach ( $custom_rules as $label => $url ) {
        $wp_rewrite->add_rewrite_tag("%" . $label . "%", '([^/]+)', $label . "=");
        $wp_rewrite->add_permastruct($label, $url, false);
      }
    
    }

    I get somewhat what I was looking for. Except, when I go to a jobs or employers page I get a page not found. It seems like the rewriting works only for news.

    * myblog.com/jobs/aerospace/aviation/16-aviation-engineer/ does NOT work
    * myblog.com/employers/aerospace/10-bombardier/ does NOT work
    * myblog.com/news/aerospace/aviation/2011/05/13-good-aviation-news/ WORKS!

    Thank you for your help.

  3. jonathanroy
    Member
    Posted 3 years ago #

    I finally decided to go with this and it works...

    add_action('init', 'create_rewrite_rules');
    function create_rewrite_rules() {
    
      global $wp_rewrite;
    
      $custom_rules = array(
        'section' => '/%section%/%category%/',
        'news' => '/%section%/%year%/%monthnum%/%news%/',
        'jobs' => '/%section%/job-%post_id%/%jobs%/',
        'employers' => '/%section%/employer-%post_id%/%employers%/'
      );
    
      foreach ( $custom_rules as $label => $url ) {
        $wp_rewrite->add_rewrite_tag("%" . $label . "%", '([^/]+)', $label . "=");
        $wp_rewrite->add_permastruct($label, $url, false);
      }
    
    }

    Keep me posted though if you find something useful for me.

Topic Closed

This topic has been closed to new replies.

About this Topic