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?
- Where, When, & How to Properly Flush Rewrite Rules Within the Scope of a Plugin?
- Custom Post Type Permalink / Rewrite not working immediately
- Best way to flush_rewrite_rules for custom post type, in a mu-plugins plugin?
- Custom rewrite rules for archive page and single post
- WordPress Rewrite Rules for Custom Post Type and Taxonomy
- CPT Template Not Showing – Getting 404
- Custom post type 404s with rewriting even after resetting permalinks
- Multiple post types – share same ReWrite slug?
- Rewrite rule for Custom post type Monthly and Yearly archive
- add_rewrite_endpoint() not working for custom post type archives
- Permalink rewrite 404 conflict- WordPress Taxonomies/ CPT
- How to modify URL structures in custom post types and taxonomies or terms
- Adding meta values to permalink
- Archive page with multiple taxonomies rewrite
- 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
- Help with a TV series Rewrite structure
- Taxonomy archives based on Custom Post Type
- 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
- How to Add Pages Under Custom Post Type URL Structure?
- Prepend meta_value to permalink of post
- How to check if last uri segment is a custom post type or taxonomy term?
- Pagination Doesn’t Work
- Rewrite custom post type url’s adding meta box values
- Permalinks using event date (year & month) instead of publication date
- Multiple parameters in a custom post type url rewrite
- Permalinks for CPT breaks permalinks to pages
- Priority for Categories, Tags, Taxonomies, Posts, Pages and Custom Posts with same url
- WP Rewrite Rules – Custom post type & taxonomy
- Custom Post Types: pretty search URLs and has_archive
- Need help with complex custom post type setup
- Custom Post Type pagination when CPT ‘rewrite’ rule and a page have the same slug
- WordPress monthly archive links result in 404
- Remove base slug in CPT & CT, use CT in permalink
- 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
- Custom Post Type rewrite redirects to homepage
- Disabling post_type in URL
- single-{post_type}.php is not loaded despite flushing rules
- Custom Permalink Tag breaks Pagination
- Adding paged query to custom URL rewrite
- WordPress rewrite my URL when i use pagination
- Custom Post Type rewrite rules not working, how to alter the rewrite order?
- Custom Permalink for Taxonimie and Custom Post Type
- how to get archive urls with same origin for custom types and terms?
- Rewrite numeric ID parameter for hierarchical custom post type
- Customising rewrite rules for CPT single post URL to work as paged URL
- Remove custom post type slug not working for child pages
- Adding the taxonomy before the custom post type
- 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
- Pagination problem
- How to rewrite custom post type with custom taxonomy urls?
- Custom post type with slug for plural (archive) and for single
- How much control do we have over CPT rewrite slugs? Can I create a “root” page for my CPT with out the page path, and then have paths for other pages?
- how to add extra GET parameters to post to url with add_rewrite_rule?
- Permalink Structure CPT With Custom Taxonomy Fails
- Parent & Child Taxonomy slugs in custom post permalink
- Rewrite WordPress URL to show CPT meta field data
- Custom Post Type won’t Register
- Making a custom help center page
- Remove url rewrites for registered taxonomies
- Custom rewrite rule for hierarchical custom post type
- Rewrite and custom post type: order by custom field not working
- Custom post type “preview” not working
- CPT custom archive template does not get loaded
- Am I using the flush_rewrite_rules function in the right place?
- Rewrite slug of custom post type to include post-id
- Display posts from Custom Post Type in category page on front-end
- 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
- Can’t get custom rewrite tag, query var, permastruct (permalink structure), and rewrite rule to work properly together
- 404 Error On Custom Taxonomy Pages 2, 3, etc
- add_rewrite_rule not working in Custom Post Type UI Plugin
- CPT Archive Pagination – Page not found
- How to add custom permalink struct with DYNAMIC author to a Custom Post Type
- add_rewrite_rule not working with custom post type
- Url rewrite with custom post type and taxonomy
- Paginate yearly archives for a custom post type
- Custom archive URL as a subfolder of the custom post type slug
- Same slug for Custom Taxonomy archive and CPT archive – Rewrite rule not working
- Two Permalinks with one Taxonomy (custom taxonomy) and (custom post type -> custom taxonomy)
- What is the best way to relate different custom post types?
- Dynamic WordPress rewrite rules for multiple custom post types
- 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 change rewrite slug for custom post type without conflicting w/ child pages of page with same slug
- Remove date rewrite rule for custom post type archive page
- how to properly format custom post type rewrite to get specific URL structure for archive and post views
- 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