WordPress.org

Support

Support » Plugins and Hacks » URL rewriting hack for custom post types

URL rewriting hack for custom post types

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘URL rewriting hack for custom post types’ is closed to new replies.
Skip to toolbar