4

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

Code1![][1]Code2

Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23

4 Answers4

3

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.

Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
Anand Shah
  • 14,575
  • 16
  • 72
  • 110
  • Thanks Anand I have done some changes but your answer was a starter for me :) – Moeed Farooqui Dec 16 '14 at 07:00
  • Can you please help me in this = I need 404 on putting wrong url. Correct is `brand/calvin-klein/tab1/watches1/` Wrong is `brand/calvin-klein/tab12231231/watches1/` but it still gives me the same error rather than a 404 page – Moeed Farooqui Dec 16 '14 at 13:49
  • 404 occurs when a page or post is not found, `tab12231231` is query variable, so you'll have to handle it inside your CPT code, check if its a valid `mtype` and if it isn't redirect to a different page or show an error message. You can manually raise a 404 , see the following thread on raising a 404 manually : http://wordpress.stackexchange.com/questions/91900/how-to-force-a-404-on-wordpress . And thanks for fixing the typo :) – Anand Shah Dec 16 '14 at 14:54
0

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

Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
0

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.

n.karlen
  • 11
  • 2
-2

before or after the call the function add_rewrite.. insert this code status_header(200);