Reputation: 3622
I want to change the permalink structure for specific custom post type.i.e
http://test.com/brand/calvin-klien?mtype=tab2
^this is dynamic
To
http://test.com/brand/calvin-klien/mtype/tab2
^this is dynamic
Here is a piece of code I tried.
Registering add_rewrite_tag
function custom_rewrite_tag() {
add_rewrite_tag('%mtype%', '([a-z0-9\-]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
add_action('init', 'wpse50530_journal_archive_rewrite', 10, 0);
Code1
function wpse50530_journal_archive_rewrite(){
add_rewrite_rule('brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$','index.php?name=$matches[1]/?mtype=$matches[2]','top');
}
Code2
add_action('generate_rewrite_rules', 'work_list');
function work_list($wp_rewrite) {
$newrules = array();
$newrules['brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$'] = 'index.php?name=$matches[1]&mtype=$matches[2]';
$wp_rewrite->rules = $newrules + $wp_rewrite->rules;
I have tried both above codes
, flushed permalinks
but still a 404
. I dont know why it is creating $matches in htaccess as htacces doesnt know WHAT IS $matches
Also I have tried monkeyman-rewrite-analyzer plugin which is showing the correct matched result for my permalink but still word press showing 404
. See attached screenshots for Code1 & Code2
Upvotes: 4
Views: 5203
Reputation: 13
Just in case anyone has the same problem and checks out this page.
In the permalink settings page, check how the permalink structure is configured. The most common is to enter /%postname%/
in the custom structure field to let the rewrite_rules work properly.
Upvotes: 0
Reputation: 47
before or after the call the function add_rewrite.. insert this code status_header(200);
Upvotes: -2
Reputation: 3622
I will continue Anand's code for some further changes. It was redirecting on name=$matches[1]
and I need to stay at the same URL I hit, for this it must include the custom post type name.
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule('^brand/([^/]*)/([^/]*)/?','index.php?brand=$matches[1]&mtype=$matches[2]','top');
^--this
}
I got the Pretty URL... Yaaay!!!
BUT URL
doesn't contain query string(i.e: mtype=tab1)
and the rest of my code is useless, so we can achieve this by doing get_query_var('mtype')
and I got the same value which was working in $_REQUEST['mtype']
and my code worked like a charm.
Also I deactivated the monkeyman pluggin
Upvotes: 0
Reputation: 14913
The following code should help
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule(
'^brand/([^/]*)/mtype/([^/]*)/?',
'index.php?name=$matches[1]&mtype=$matches[2]',
'top');
}
Flush your permalinks and it should work.
Upvotes: 3