Title: Custom post rewrite
Last modified: August 21, 2016

---

# Custom post rewrite

 *  [Piotr Po](https://wordpress.org/support/users/potreb/)
 * (@potreb)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/custom-post-rewrite/)
 * 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](https://wordpress.org/support/users/potreb/)
 * (@potreb)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/custom-post-rewrite/#post-4447029)
 * 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](http://pastebin.com/reYMZKPN)
 * 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.

## Tags

 * [WP_Rewrite](https://wordpress.org/support/topic-tag/wp_rewrite/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Piotr Po](https://wordpress.org/support/users/potreb/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/custom-post-rewrite/#post-4447029)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
