php – WordPress is removing query variables like page, p from the URL of a custom static page


I am working on a custom template and displaying some posts (of custom type) teasers on it. In the template PHP file, I want to retrieve a URL query variable (if passed) and display the content accordingly. The URL with the query variable is generated from the same template page. If I try something like this to generate URL:

<?php echo esc_url( add_query_arg( 'custom_var', '2' ) ); ?>

and in functions.php:

function add_custom_query_var($vars) {
    $vars[] = "custom_var";
    return $vars;
add_filter('query_vars', 'add_custom_query_var');

and go to, it work as expected and I can get the value using get_query_var function. The problem is that when I replace this custom_var with page or p, these variables are removed from the URL automatically when the page is loaded. In short a URL like: is redirected to:

Can we not use these variables in custom templates? Or is there any way to do so? And I am not looking for pagination, this page variable is meant for totally custom logic. I know the WP_Query class also use page variable and I am sure there is something conflicting. Any help would be appreciated.

Imran Aslam 1 month 2022-08-20T12:49:18-05:00 0 Answers 0 views 0

Leave an answer