0

The site that I'm working on uses a custom theme built by me.

  • My Homepage is a static page, which uses the file front-page.php.
  • My index.php is only used for "blog type" of pages.

I want to change some contents in my site using queries (ex. ?country=US) and it is working fine in all pages except for the Homepage. When I send a query, it loads the index.php instead of the front-page.php.

An interesting behaviour is that, when I use a non-existing variable (ex. ?asdfg=asdfg) it seems to ignore the query and it loads the front-page.php as expected. But when I send an existing variable (ex. ?country=US), it breaks and loads the index.php.

function custom_query_vars($vars)
{
    $vars[] = 'country';
    return $vars;
}
add_filter('query_vars', 'custom_query_vars');

I've already tried:

  1. Creating a custom page theme (page-home.php) and setting it for my "Home" page which is my static homepage. It would still load the index.php when passing parameters.

  2. "Forcing" the use of the front-page.php theme when I'm on the home page and getting the country variable:

function custom_front_page_template($template) {
    if (is_front_page() && isset($_GET['country'])) {
        // If it's the homepage and the 'country' query parameter is set
        return locate_template('front-page.php'); // Load front-page.php
    }
    return $template; // Otherwise, use the default template
}
add_filter('template_include', 'custom_front_page_template', 99);

This would still load the index.php file, with broken CSS.

Edit: I tried both is_home() and is_front_page() in the conditional above.

Do you have any ideas on what is going on? Thank you in advance!

3
  • what does query monitor/the body css classes claim the page is? Does WP consider it the homepage, frontpage, or something else? "Home" and "Frontpage" have very similar but distinct definitions, likewise check home.php. Template loading is also a little different for the homepage and can override what's set on the page you set as the homepage. It's very likely that by adding a query var, WP no longer considers it to be the homepage
    – Tom J Nowell
    Oct 5, 2023 at 12:53
  • Hello @TomJNowell ! When I don't add a query var, it is the frontpage: I used <?php echo is_front_page();?> to verify, and it prints "1" while is_home() prints nothing. However, when I use a query var, it doesn't print anything. In fact, I went to my index.php and wrote: <?php global $post; $post_slug = $post->post_name; echo $post_slug; ?> And this prints the page slug of my last post.
    – Julia
    Oct 5, 2023 at 13:24
  • your question explicitly states that you do: ?country=US, I think there's been a misunderstanding about what the front page is, and it has a much stricter more specific meaning than you've interpreted it to have. If you use a URL parameter that is a query variable then it is no longer the "Front". Note that it doesn't matter if the pretty part of the URL is just /, the entire thing is considered, hence why /?s=test loads the search template, not the homepage template. Also home.php and front-page.php don't do the same thing, it is possible for the frontpage to not be the homepage
    – Tom J Nowell
    Oct 5, 2023 at 13:47

1 Answer 1

0

There are some fundamental misunderstandings happening here, but importantly:

  • The variables on the main query are what are used to decide which conditionals are true. E.g. will is_home return true, is is_search true? It does this by inspecting the query variables.
    • It's these conditionals and variables that are then used to decide which template should be loaded.
  • For some reason you've specified that country is a query variable, therefore if it's defined it cannot be the homepage.
    • This is why random parameters don't trigger this, because they are URL parameters, but they aren't ingested into WP_Query and affect the main post query
    • This is also why /?s=test will load search.php instead of home.php or front-page.php
  • If I create a page, give it a page template, then set it as the blog or homepage, that page template I set is ignored and does not factor into the decision making about which template to use.
  • The front page is not your homepage. In a lot of situations it is, but that is coincidental.
    • The front page is the first page of the default post archive.
    • On most sites this just happens to be the homepage, but it can be moved
    • The homepage template is home.php, not front-page.php.

By default the homepage and the frontpage are the root of your site, and share the same URL, but if you use these options their locations change:

enter image description here

The frontpage is what you chose for "Posts", and specifically that URL, the second page or customised versions with additional query parameters don't count.

The most direct solution to your problem, is to not add country as a query variable, especially since you aren't using it that way either and reading the value directly from $_GET, which suggests it's being misused.

Query variables are meant to be used for setting parameters on WP_Query to aid in rewrite rules and extending support for database clauses. They're not meant to be used to read in URL parameters from forms or options. E.g. if you wanted to hide ?country=US and replace it with country/US via a rewrite rule, then a query variable makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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