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:
- 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
- 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
- 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
- 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
- Different rewrite rules for taxonomies and cp in multisite?
- How to add custom taxonomy in custom post type permalink?
- Add parent post to Custom Post Type [closed]
- Relationship between set rewrite flag in register_post_type and add_rewrite_rule
- Custom rewrite to fetch feed data
- custom post type archive page has 404 error even though has_archive is true
- Custom Permalinks for Custom Post Types
- 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
- SEO Concerns With Rewrite Rules
- Route subpage to Custom Post Type
- How to rewrite custom taxonomy term archive to use THE SAME SLUG/front as CPT
- Rewrite permalink for multiple post types (CPT) with taxonomy term
- rewriterule not working
- Pagination for custom post types – url rewriting
- Custom Post Type with modified permalink structure results in 404
- Modifying rewrite rules to use /category//
- Custom post rewrite rule not working
- Subpages Permalinks Issues
- rewrite rules hierarchical
- Custom Post Type Date Based Archive URL rewrite [duplicate]
- How to make a post accessible from multiple post type slugs?
- multiple URLS for the same page
- Rewrite URL for CPT to domain/post-name/post-id
- How to apply multiple rewrite_rule to a single custom post type url?
- Category permalinks don’t work even after flushing
- Custom permalink structure for custom post types including multiple taxonomy
- CPTs relationship
- Custom Post Type parent/child relationship rewrite rules for permalinks
- Rewrite url for custom post type with specific url
- Default custom post type url
- How to build overlapping custom post_type and taxonomy rewrites without conflicts?
- Two CPT: one is using part of the custom rewrite slug from the other
- Template hierarchy changes when custom post-type permalink leverages custom taxonomy
- Adding Custom Taxonomy Along-with Post Name in Post Type Permalink
- Associate custom CPT with another CPT’s taxonomy and rewrite permalinks
- Permalinks for a custom post type – with a page template (custom query) and taxonomy archive
- how to use two permalinks for one custom post type based on categories
- WordPress custom post type rewrite rule matches all pages returning 404
- page not found for example.com/custom-post-name
- Get the custom post type slug but remove the rewrite prefix?
- Rewrite for custom type and custom taxonomy
- Custom Rewrite Tag Structure
- Why won’t this rewrite rule work?
- Rewrite rules causing hole
- Parent cpt/child custom post type URL permalink relationship
- CPT Template Not Showing – Getting 404
- 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
- Adding rewrite rules to Custom Post-type mixed with Taxonomy
- Custom post type permalink structure
- 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
- How to add random prefix (based on related page slug) to custom post type?
- How does WordPress manage to differentiate between post and page URLs without a distinct base, and how can I replicate this functionality?
- Rewrite taxonomy permalink appended to CPT archive url