My question is very simple. I created a new custom page template called page-printable.php. Then, I created a page with slug printable. If I access the page with localhost/printable or localhost/index.php?pagename=printable, it shows my custom page with no error. However, my custom page uses $_GET internally. But if I access the page with localhost/printable?p_year=2018 or even with no-pretty url (localhost/index.php?pagename=printable&p_year=2018), instead of returning the correct custom page with the queries at $_GET, it returns a 404 not found.

Here is the code of my page-printable.php:

<html <?php language_attributes() ?>>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link href="https://fonts.googleapis.com/css?family=Mukta+Vaani:400,600,700|Roboto+Mono:500,700"
    <link rel="stylesheet" href="http://localhost/frontend/dist/assets/css/main.css" type="text/css">
<?php echo $query ?>
<div class="una-printable">

if (!empty($_GET['p_year'])) {
    $query['year'] = $_GET['p_year'];

if (!empty($_GET['p_month'])) {
    $query['month'] = $_GET['p_month'];

$query = !empty($query) ? "?" . http_build_query($query) : '';

$events = json_decode(file_get_contents("http://dev.unasp.edu.br/wp-json/custom_api/v1/printable" . $query), true);
$month_name = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];

foreach ($events as &$months) {

    foreach ($months as &$days) {

foreach ($events as $year => $months) : ?>
    <hr />
    <div class="year">
        <h1><?php echo $year ?></h1>
    <hr />

    <?php foreach ($months as $month => $days) : ?>
        <h2><?php echo $month_name[$month - 1] ?></h2>

        <?php foreach ($days as $day => $events) : ?>
            <?php foreach ($events as $event) : 
            $date = date('Y-m-d', strtotime("{$year}-{$month}-{$day}")) ?>
                    <?php echo date('d/m', strtotime($date));

                    if ($event['end'] !== $date) {
                        echo " – " . date('d/m', strtotime($event['end']));
                <td><?php echo $event['name'] ?></td>
                        <?php foreach ($event['unities'] as $unity) : ?>
                            <li><?php echo $unity ?></li>
                        <?php endforeach ?>
            <?php endforeach;
        endforeach; ?>
    <?php endforeach;    
endforeach ?>
const fn = () => window.print()

if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
} else {
    document.addEventListener('DOMContentLoaded', fn);

How can I handle this?

As suggested by Aadil P., I tried to use query_vars filter to add my queries, as the following code:

add_filter('query_vars', function ($qv) {
    $qv[] = 'p_year';
    $qv[] = 'p_month';
    return $qv;

I did not forget to flush rewrites in Permalink pages. Unfortunately, It didn't make any difference.

Thanks in advance.

  • Have you tried localhost/printable/?queryvar=queryvalue? – Nathan Johnson Feb 18 '18 at 17:56
  • @NathanJohnson Yes, I did. It also returns 404. – Mateus Felipe Feb 18 '18 at 18:18
  • @MateusFelipe which queryvar exactly did you use? You may very well be messing around with the main query by reusing parameters that WP_Query uses, e.g. p, please update your question with a specific example rather than a placeholder like example/foo/queryvar – Tom J Nowell Feb 18 '18 at 19:20
  • The exact query I used was year=2018, that is being used internally to filter results by year. So the full url was localhost/index.php?pagename=printable&year=2018. – Mateus Felipe Feb 18 '18 at 19:23
  • 1
    year is a built-in query var that WordPress uses for date archives, you are unintentionally modifying the main query by adding that in the query string. You'll have to use something unique for your own custom query var. – Milo Feb 19 '18 at 4:08

Wordpress rewrites standard wordpress urls from /category/post-name/ or whatever your selected permalink type is, into index.php?....

You are adding query vars to a link that is already getting rewritten into index.php?... and thus you are getting a 404.

My advice to you would be to use something similar to an already asked question at Custom page template query_vars or register the query vars properly in Wordpress.

Wordpress developer reference for adding a query argument -> https://developer.wordpress.org/reference/functions/add_query_arg/

I believe that this should answer your question about how you can handle this.

|improve this answer|||||
  • Well. If I access localhost/index.php?pagename=printable it returns the correct page. Shouldn't localhost/index.php?pagenanme=printable&otherquery=queryval map otherquery to $_GET? It doesn't works. – Mateus Felipe Feb 18 '18 at 18:21
  • Wordpress ignores query strings un-registered with it and hence that is why your vars are being ignored. Edit - You should refer to this wonderful stack exchange thread I used some while back when I had the similar issue (stackoverflow.com/questions/6683666/…) – Aadil P. Feb 18 '18 at 19:12
  • I can't get how add_query_arg() will help me here... – Mateus Felipe Feb 18 '18 at 19:20
  • I tried to add year to my query_vars as written in the link you passed and flushed the rewrite_rules, but still didn't work. – Mateus Felipe Feb 18 '18 at 19:32
  • I believe that it's time to ask you to share your code . – Aadil P. Feb 19 '18 at 6:11

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.