0

I have been reading around and cannot seem to figure this out.

Everything is working fine except that pagination seems to not work, I am adding a second rewrite rule, shouldn't this fix that?

I can get to the first page fine, but any page beyond throws a 404.

Here is my code:

// Rewrite the Sermon URL
function sermon_rewrite() {
    $cpt = 'sermon';
    $tax = 'series';

    add_rewrite_rule(
        '^'.$cpt.'/([^/]+)/([^/]+)/([^/]+)/?',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$tax.'=$matches[2]&'.$cpt.'=$matches[3]',
        'top'
    );
    add_rewrite_rule(
        'sermon/([^/]+)/page/?([0-9]{1,})/?$',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&paged=$matches[2]',
        'top'
    );
}
add_action('init','sermon_rewrite');

EDIT

If I go to the following url

This works: mysite.com/sermon/term/childterm/page2/

This does not: mysite.com/sermon/term/childterm/page2/

Any idea what is causing that?

0

This works: mysite.com/sermon/term/childterm/page2/

This does not: mysite.com/sermon/term/childterm/page2/


according to your rewrite rule, I guess what you mean is

This works: mysite.com/sermon/term/page2/

This does not: mysite.com/sermon/term/childterm/page2/

I'm not a regex expert so I only come up with adding another rewrite rule to handle the childterm situaion

add_rewrite_rule(
        'sermon/(.*?)/(.*?)/page/?([0-9]{1,})/?$',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[2]&paged=$matches[3]',
        'top'
    );
0

Figured it out. I had everything right, it just was not being called in the proper order. I should have been matching paged url's first before the post.

This is my working code:

// Rewrite the Sermon URL
function sermon_rewrite() {
    $cpt = 'sermon';
    $tax = 'series';

    add_rewrite_rule(
        '^'.$cpt.'/([^/]+)/page/([0-9]+)/?$',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&paged=$matches[2]',
        'top'
    );
    add_rewrite_rule(
        '^'.$cpt.'/([^/]+)/([^/]+)/([^/]+)/?',
        'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$tax.'=$matches[2]&'.$cpt.'=$matches[3]',
        'top'
    );
}
add_action('init','sermon_rewrite');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.