I have a custom post type called news and in order to distinguish current and old news posts I have a custom field where the client can mark a news post as ‘archived‘.

So my permalink rewrite code looks like this:

function custom_rewrite_rule() {
    add_rewrite_rule('^news/archive/?','index.php?post_type=news‌​&news_archive=true',‌​'top'); 
    add_rewrite_rule('^news/archive/page/([0-9]+)?/?$','index.php?post_type=news&news_archive=true&paged=$matches[1]','top');   
}
add_action('init', 'custom_rewrite_rule', 10, 0);

function add_query_vars_filter( $vars ){
    $vars[] = "news_archive";
    return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

The first bit works but the problem is with the pagination. I can access the news_archive query_var but not the paged query_var.

What is the correct way to incorporate paged into this permalink rewrite?

1 Answer
1

The problem is that your first rule is capturing anything that begins with news/archive/.

Add the $ anchor to match only news/archive/:

add_rewrite_rule(
    '^news/archive/?$',
    'index.php?post_type=news&news_archive=true',
    'top'
);

Your pagination rule will then begin to work as-is after you flush permalinks.

Leave a Reply

Your email address will not be published. Required fields are marked *