• NOTE: I can’t publish web pages as they’re in a non-public environment. Feel free to move it into “developing with WordPress” if it’s too technical.

    Hello, I’m struggling with search block and search results page, in TwentyTwentyfour theme.

    Personal need: blog is based on two sections, each one has its posts categorized as “real world” and “fake world”. No particular customization in types and taxonomies, “it’s just “real world” and “fake world” are two categories with their own customized templates – fakepost and realpost, associated to their own template parts (headers and footers, meta and sidebars).

    For the search block I changed its behaviour manually by code view, in each header, as follows:

    <!-- wp:search {"showLabel":false,"buttonPosition":"button-only","buttonUseIcon":true,"query":{"cat":"xxx"}} /-->

    “xxx” stands for the numeric ID corresponding to the desired category, let’s say “10” for real, “11” for fake.

    This way, when I search for a keyword, it returns [siteurl]/?s=[keyword]&cat=[number] – this is what I wanted it to do.

    But the issue comes in the results page!

    • template parts are the default ones (good just for “real world” section);
    • the search block included in results page, is obviously not customized.

    Is there a way to tell the search form’s “get” method, ‘if search comes from /real go to result-real, if it comes from /fake, point to fake-results’?”

    I googled and found that for customizing search template you should create a searchform.php file but, besides not working (I uploaded it into /templates/ folder), I don’t find the right instructions to build the condition to influence the form submission. And I don’t feel like changing the /wp-includes/general-template.

    Thanks in advance for possible help

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi Elena!
    I don’t have a clear answer to provide but happy to help you find a solution.
    Could you provide a repo to replicate your specific use case? Maybe a child theme of TT4?
    It would be easier to work on possible solutions from a repo.

    Thread Starter Elena Brescacin

    (@talksina)

    Hello, wait – a repo, you mean?

    Should I backup my theme then upload it into github somehow?

    Should I backup my theme then upload it into github somehow?

    Having a basic structure of a theme (or child theme) replicating your use case (nothing fancy, just the minimum set of files required to replicate the setup explained above) in a public GitHub repository would be useful for me to help with this issue.

    Thread Starter Elena Brescacin

    (@talksina)

    I have it – child theme 2024

    I have just uploaded the zip file of theme without doing anything else – I am really new at github publishing, I have just used it to report issues

    Hi Elena!
    After doing some research, I think a good way to approach your use case would be:
    1- creating a custom template for each one of the versions you’d like to have for the search results.

    /templates
    results-fake.html
    results-real.html

    2- Adding the following code so the search is filtered and depending on the category ID (8 for category “fake” and 7 for category “real” in the example) a different template is returned:

    add_filter('search_template_hierarchy', function ($templates) {

    if (isset($_GET['cat'])) {
    if (8 === absint($_GET['cat'])) {
    $templates = [ 'results-fake.html' ] + $templates;
    } elseif (7 === absint($_GET['cat'])) {
    $templates = [ 'results-real.html' ] + $templates;
    }
    }
    return $templates;
    });

    With this implementation the search /?s=&cat=8 will return results-fake.html template and /?s=&cat=7 will return results-real.html template.

    Hope this helps!

    ?? Kudos to @ndiego and @greenshady for their help with this question

    Thread Starter Elena Brescacin

    (@talksina)

    Hello, sorry for delay but I had no computer to test this plugin on.

    The problem is that I do not see what I expect – I mean instead of saying “result for [query]” on the title, in the production site it says the name of the category. And it happened since I updated WP to 6.6 and 2024 theme.

    While in the backup site it works.

    You can try it yourself without even going to the search, just typing the query link directly:

    Backup/test site:

    Italian: https://backup.plusbrothers.net/?s=coming&cat=134

    English: https://backup.plusbrothers.net/?s=coming&cat=118

    134 is ID for category “Italian” while “118” is for “international”.

    While in the production site:

    https://plusbrothers.net/?s=coming&cat=134 and English parameter just replace s=coming with s=start, and cat=118…

    the title of the page comes out with the category’s name instead of the query’s title “results for: [query]”.

    I also added one more control in there: if the category is 118, that is international, it switches locale -frontend interface- to en_US and if it loads the Italian, it changes to it_IT.

    Working on all of this though, I noticed that the category parameter isn’t so reliable, because it can create some issues. So, I was wondering if you can give me furter help to fix them.

    The conditions are: current template the user is visiting, has specific parts (headers and footers).

    So, if the header slug is just “header” (Italian) and the footer slug is “footer” (italian):

    • in case of search, it filters post to category Italian (id=134) and loads “search.html” (Italian template);
    • changes locale in it_IT in any post, page, post-type;
    • in case of 404 error, load 404-it page.

    If header and footer slugs are English-header and english-footer…

    • in case of search, it filters post to category International (id=118) and loads “english-search.html” (English template);
    • changes locale in en_US in any post, page, post-type;
    • in case of 404 error, load 404-en page.

    I think this would be a silly if…else conditioning trick but not being too skilled, I hardly get it.

    Thanks for possible help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.