I generate virtual pages (using index.php), which look like this:
domain/died-persons/2024/
where 'died-persons' is a static page with a table of contents (list of years) and '2024' is a virtual directory (variable) which I manage by a rewrite rule. This works fine whether I'm logged in or not.
When I try to open paginated pages like this:
domain/died-persons/2024/page/2/
it only works, when I'm logged in. When I'm logged out, the page isn't found and I get a 404 error.
In functions.php I have this:
function year_add_query_vars( $vars ) {
$vars[] = 'variable';
return $vars;
}
add_filter( 'query_vars', 'year_add_query_vars' );
add_rewrite_rule('died-persons/([0-9]{4})[/]page/([0-9]{1,})/?$', 'index.php?variable=$matches[1]&paged=$matches[2]', 'top');
In index.php I have this condition for getting the template:
if ( preg_match('#died-persons/([0-9]{4})/page/([0-9]{1,})/?$#', $_SERVER['REQUEST_URI'])) {
get_template_part( 'template-parts/content-personlist' );
}
In content-personlist.php I generate the list of items (persons), which I'm getting from an external database.
I would be happy if someone could help me with this problem. Thanks in advance!
Andreas
I generate virtual pages (using index.php), which look like this:
domain/died-persons/2024/
where 'died-persons' is a static page with a table of contents (list of years) and '2024' is a virtual directory (variable) which I manage by a rewrite rule. This works fine whether I'm logged in or not.
When I try to open paginated pages like this:
domain/died-persons/2024/page/2/
it only works, when I'm logged in. When I'm logged out, the page isn't found and I get a 404 error.
In functions.php I have this:
function year_add_query_vars( $vars ) {
$vars[] = 'variable';
return $vars;
}
add_filter( 'query_vars', 'year_add_query_vars' );
add_rewrite_rule('died-persons/([0-9]{4})[/]page/([0-9]{1,})/?$', 'index.php?variable=$matches[1]&paged=$matches[2]', 'top');
In index.php I have this condition for getting the template:
if ( preg_match('#died-persons/([0-9]{4})/page/([0-9]{1,})/?$#', $_SERVER['REQUEST_URI'])) {
get_template_part( 'template-parts/content-personlist' );
}
In content-personlist.php I generate the list of items (persons), which I'm getting from an external database.
I would be happy if someone could help me with this problem. Thanks in advance!
Andreas
Share Improve this question edited Mar 17 at 12:43 Andreas Böttcher asked Mar 17 at 11:44 Andreas BöttcherAndreas Böttcher 111 silver badge3 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0Flush Rewrite Rules
Add this temporarily in
functions.php
and visit the site once:add_action('init', function() {
flush_rewrite_rules();
});
Remove it afterward.
Ensure Query Vars Are Passed
Modify your query vars filter:
function year_add_query_vars($vars) {
$vars[] = 'variable';
$vars[] = 'paged';
return $vars;
}
add_filter('query_vars', 'year_add_query_vars');
Fix Permalink Structure
- Check if Settings → Permalinks is set to "Post Name" and save.
Check Server Cache
- If using a caching plugin or Cloudflare, clear the cache.
query_vars
hook, so I would encourage you to useget_query_var
inindex.php
to check for its existence. – Chris Haas Commented Mar 17 at 14:19paged
is a special internal WordPress variable, however you are trying to use it with your own custom variable. I can't say this for absolute certain, but I wouldn't be surprised if this is confusing WordPress. Try using your own variable for pagination that isn't listed in codex.wordpress./WordPress_Query_Vars – Chris Haas Commented Mar 17 at 14:26