• Hi. I have some problem with custom post rewriting:

    function permlink() {
    	global $wp_rewrite;
    	$wp_rewrite->add_rewrite_tag("%faq%", '([^/]+)', "faq=");
    	$wp_rewrite->add_permastruct('faq', 'faq/%category%/%faq%.html', false);
    
    	$wp_rewrite->add_rewrite_tag("%contract%", '([^/]+)', "contract=");
    	$wp_rewrite->add_permastruct('contract', 'contract/%category%/%contract%.html', false);
    	$wp_rewrite->flush_rules();
    }
    add_action('init', 'permlink');

    I need to create links like %category%/%postname%.html for all post types (also for default post, page). Is it possible?

Viewing 1 replies (of 1 total)
  • Thread Starter Piotr Po

    (@potreb)

    I found the solution:

    $wp_rewrite->add_rewrite_tag("%faq%", '([^/]+)', "faq=");
    	$wp_rewrite->add_permastruct('faq', '%category%/%faq%.html', false);
    	$wp_rewrite->add_rewrite_tag("%contract%", '([^/]+)', "contract=");
    	$wp_rewrite->add_permastruct('contract', '%category%/%contract%.html', false);

    First we add rewrite rule for own custom posts. When we reflushing url we will see error 404.

    For this problem we need change pre_get_posts function:

    function rewrite_parse_post( $query ) {
    	if ( ! is_admin() && $query->is_main_query() ) {
    		$query->set('post_type', array('post', 'faq', 'contract'));
    		return $query;
    	}
    }
    add_filter( 'pre_get_posts', 'rewrite_parse_post' );

    I’m also using function which remove category slug and change after rewriting permatructure post_type_link.

    Shared on pastebin

    This is a better solution, because we keep a standard for rewriting urls.

    For all post types I’m also added this same categories like for posts.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom post rewrite’ is closed to new replies.