WP_Post_Type::add_rewrite_rules() public WP 4.6.0
Adds the necessary rewrite rules for the post type.
{} It's a method of the class: WP_Post_Type{}
No Hooks.
Return
null
. Null. Nothing.
Usage
$WP_Post_Type = new WP_Post_Type(); $WP_Post_Type->add_rewrite_rules();
Notes
- Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.
- Global. WP. $wp Current WordPress environment instance.
Changelog
Since 4.6.0 | Introduced. |
Code of WP_Post_Type::add_rewrite_rules() WP Post Type::add rewrite rules WP 5.7.1
public function add_rewrite_rules() {
global $wp_rewrite, $wp;
if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
$wp->add_query_var( $this->query_var );
}
if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) {
if ( $this->hierarchical ) {
add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" );
} else {
add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" );
}
if ( $this->has_archive ) {
$archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive;
if ( $this->rewrite['with_front'] ) {
$archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
} else {
$archive_slug = $wp_rewrite->root . $archive_slug;
}
add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' );
if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) {
$feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' );
}
if ( $this->rewrite['pages'] ) {
add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' );
}
}
$permastruct_args = $this->rewrite;
$permastruct_args['feed'] = $permastruct_args['feeds'];
add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args );
}
}