The problem is that when the main query is parsed, having just a custom query var set will not result in a successful main query, so it defaults to setting up the home page blog posts query.
To solve this, set the post type query var directly in the rewrite rule and there’s no need for those intermediate steps-
add_rewrite_rule('slp\/(.*)', 'index.php?speaker=$matches[1]', 'top');
You will also need to filter post_type_link
if you want to hide the original URL when permalinks are output by API functions.
function wpd_speaker_links( $url, $post ){
if ( 'speaker' == get_post_type( $post ) ) {
return home_url( "/slp/" . $post->post_name . "https://wordpress.stackexchange.com/" );
}
return $url;
}
add_filter( 'post_type_link', 'wpd_speaker_links', 10, 2 );
Related Posts:
- Can a custom post type have a Parent Page?
- Custom Post Type Permalink / Rewrite not working immediately
- Best way to flush_rewrite_rules for custom post type, in a mu-plugins plugin?
- Multiple post types – share same ReWrite slug?
- add_rewrite_endpoint() not working for custom post type archives
- How to modify URL structures in custom post types and taxonomies or terms
- Modify custom post type rewrite rules in a separate function
- Permalinks so that one custom post type appears to be a child of another, not working
- How to achieve this permalink -> category-name/custom-post-type-name/post-name
- Change page /2 to /transcript with a Rewrite
- Using %postname% tag with a Custom Permastruct creates 400 Bad Request Errors from the server
- WordPress Custom Post Type Category Page
- Permalink rewrite with custom post type and custom taxonomy
- Prepend meta_value to permalink of post
- Rewrite custom post type url’s adding meta box values
- Multiple parameters in a custom post type url rewrite
- Priority for Categories, Tags, Taxonomies, Posts, Pages and Custom Posts with same url
- Need help with complex custom post type setup
- WordPress monthly archive links result in 404
- Rewrite rules for custom post type ‘attached’ to another custom post type
- wrong template for page of archive
- WordPress Rewrite rule for nested post types
- how to get archive urls with same origin for custom types and terms?
- Add hierarchical taxonomy to permalink for custom post type
- How to Combine Two Custom Post Types into Single Permalink Structure
- add_rewrite_endpoint() and Custom Post Type Archive
- How to rewrite custom post type with custom taxonomy urls?
- Rewrite WordPress URL to show CPT meta field data
- Custom Post Type won’t Register
- Custom rewrite rule for hierarchical custom post type
- Rewrite and custom post type: order by custom field not working
- Rewrite slug of custom post type to include post-id
- How to change custom post type permalink structure
- Front custom edit post page for each post
- How can I use get_post_meta with add_rewrite_rule to build custom permalinks?
- Adding Post id to the end of slug in a Custom Post Type
- Rewrite Question Mark in Post Template to Make it Pretty URL
- Custom taxonomy rewrite with query var returns %2F in URL
- cpt and page sharing same base permalink
- Hide or disallow default custom post type url?
- A case for Hierarchical Custom Posts
- CPT Template Not Showing – Getting 404
- Rewrite CPT child page URL to support query var
- How to get list of taxonomy slugs ordered parents>childs?
- Rewrite Endpoints and CPTs – How to use in a plugin
- Permalink structure with custom taxonomies and custom post type like /parent-tax/child-tax/custom-post-type-name (with no base name)
- Flush Rewrite Rules when new site is created on network
- Pretty URLs and custom post types
- Specific routing for CPT
- Best structure / rewrite rules to achieve the following url
- Properly flush rewrite rules on plugin activation
- Relationship between set rewrite flag in register_post_type and add_rewrite_rule
- Custom Rewrite Rule for Custom Post Type with URL Param
- WordPress custom slug rewrite
- Custom post type paging 404 error
- Custom Post Type posts, conflict with posts after add_rewrite_rule to top
- Combine multiple CPT names to create valid permalinks
- Custom Post Type – Rewite Archive page
- How to rewrite custom taxonomy term archive to use THE SAME SLUG/front as CPT
- How to rewrite the post slug of a custom post type post
- rewriterule not working
- Custom post types and custom variables — add_rewrite_tag() not working
- Add Custom Values to Permalinks Through Custom Fields and Posts
- Why do I need to register my custom post type a second time when flushing rewrite rules?
- Custom post rewrite rule not working
- Subpages Permalinks Issues
- rewrite rules hierarchical
- Rewrite rules for custom post type
- Custom taxonomy and post type rewrite rules
- WordPress rewrite rule doesn’t work unless post category is explicitly called in regex
- Url Rewriting a dynamic wordpress page with parameters
- How to make a post accessible from multiple post type slugs?
- Rewrite URL for CPT to domain/post-name/post-id
- Custom Post Type add_rewrite_rule
- How to apply multiple rewrite_rule to a single custom post type url?
- Custom permalink structure for custom post types including multiple taxonomy
- Sudden 404 pages on product category archives
- CPTs relationship
- Change CPT permalink to use the category
- add_rewrite_rule wordpress ignoring url pattern, wp rewrite not working
- Rewrite url for custom post type with specific url
- Pagination URL in Custom Content Type
- Two CPT: one is using part of the custom rewrite slug from the other
- Custom taxonomy archive slug overwrites static page
- Group based routing and administration
- Adding Custom Taxonomy Along-with Post Name in Post Type Permalink
- how to use two permalinks for one custom post type based on categories
- Get the custom post type slug but remove the rewrite prefix?
- Custom Rewrite Tag Structure
- Custom post type child page 404
- Parent cpt/child custom post type URL permalink relationship
- Custom post type rewrite with_front
- How to make nested custom post type slugs work
- Rewrite Rule & Regex – Extract ‘cat-name’ and ‘page-number’ from http://domain.com/custom-post-type/category/cat-name/page/page-number
- CPT Template Not Showing – Getting 404
- Permalink of Custom Post Type
- add_rewrite_rule not working in Custom Post Type UI Plugin
- How to add custom permalink struct with DYNAMIC author to a Custom Post Type
- Custom archive URL as a subfolder of the custom post type slug
- Need help creating a WordPress site that has a landing page and sub pages for an area (for example, “London”). Website will have 100’s of areas