Archived topic
search results page: repeating the user's query
15 replies · Started by Leslie on January 17, 2022
Hi, I am trying to style my search results page with a title like this:
Search results for: [user's query goes here].
Here is what I have in my child theme search.php file:
<header class="page-header">
<h1 class="page-title">
<?php
printf(
/* translators: 1: Search query name */
__( 'Search Results for: %s', 'generatepress' ),
'<span>' . get_search_query() . '</span>'
);
?>
</h1>`
</header>
Here is an example page:
https://staging-lexata.kinsta.cloud/?s=interim
Can you tell me what I'm doing wrong? I've tried a bunch of things and can't figure it out. Thanks!
Hi Leslie,
Not sure I fully understand you question.
What exactly are you trying to achieve?
Did you look at the link I sent? It shows nothing after "Search results for". I am trying to achieve what I stated above: repeat the user's query so it says "Search results for: interim", in the case of the example I sent.
Hi Leslie,
I've checked the page but it returns a critical error message so I'm unable to check.
Also,
The theme's search result page should already have this.
By default, the search page has this same template -
https://github.com/tomusborne/generatepress/blob/df0190c5f0ac485cb78cb7c2e01365758c4c9d30/inc/structure/archives.php#L119-L142
So there shouldn't be any need to do it. Were you trying to move it out? Can you share how your child theme templates are structured?
The link should work for you now. The reason it was showing a critical error message is because I was trying different things, unsuccessfully. I see your point that the default should do what I want. The reason I have a child theme search.php is because I am sending my users' searches to an external api (OpenAI) for better semantic results. My file structure is:
/wp-content/themes/generatepress_child/search.php. I tried removing all of the code from my original question above, to see if the default would kick in, but it did not. So I think I might need to copy the default code into my child search.php. However, I don't know how much of it to copy, or where exactly to put it. Thanks for your help.
Can you share the exact code on the search.php template you're using?
Also, can you try hooking:
var_dump(get_search_query()); on generate_after_header hook on the search page. To check the function itself is returning something.
This way, we can have a clue if it's the search query that's having conflict with something. In this case, you may have to disable some plugins to see which one is causing it.
Hi, I added the hook and it returns this: string(7) "interim".
Here is my child theme search.php:
<?php
/**
* The template for displaying Search Results pages.
*
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
get_header();
global $wp_query;
$wp_query = openai_search();
/**
* For testing, please do not delete
echo '<pre>';
print_r($wp_query);
echo '</pre><hr/>';
*/
?>
<div>>
<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
<?php
/**
* generate_before_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_before_main_content' );
if ( generate_has_default_loop() ) {
if ( $wp_query->have_posts() ) :
?>
<header class="page-header">
<h1 class="page-title">
<?php
printf(
/* translators: 1: Search query name */
__( 'Search Results for: %s', 'generatepress' ),
'<span>' . get_search_query() . '</span>'
);
?>
</h1>
</header>
<?php
while ( $wp_query->have_posts() ) :
the_post();
generate_do_template_part( 'search' );
endwhile;
/**
* generate_after_loop hook.
*
* @since 2.3
*/
do_action( 'generate_after_loop', 'search' );
else :
generate_do_template_part( 'none' );
endif;
}
/**
* generate_after_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_main_content' );
?>
</main>
</div>
<?php
/**
* generate_after_primary_content_area hook.
*
* @since 2.0
*/
do_action( 'generate_after_primary_content_area' );
generate_construct_sidebars();
get_footer();
Can you try moving this:
<header class=”page-header”>
<h1 class=”page-title”>
<?php
printf(
/* translators: 1: Search query name */
__( ‘Search Results for: %s’, ‘generatepress’ ),
‘<span>’ . get_search_query() . ‘</span>’
);
?>
</h1>
</header>
Before this?
if ( generate_has_default_loop() ) {
if ( $wp_query->have_posts() ) :
as this header should be outside of the loop.
Unfortunately I can't get it to work. First I tried moving the code as you requested, which didn't work, so I tried about 20 different combinations and permutations of where to put it, none of which worked. Should I separate the php from the header element? Should it go below the loop? This seems like it should be pretty straightforward but clearly not. Let me know if you can come up with any other suggestions. Thanks.
There shouldn't be any need to make another file.
The reference you need are these 2 files.
https://github.com/tomusborne/generatepress/blob/master/archive.php
https://github.com/tomusborne/generatepress/blob/master/inc/structure/archives.php
The /inc/structure/archives.php makes changes to archive.php. it's mostly actions and filters.
Can you let us have a look at the backend to check a few things? You can use the private information text field for this - https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
It may be worth asking OpenAI's support if they have a preferred method in showing the searched term besides get_search_query().
Thanks again,
1. I will look at those two links you sent.
2. Given the number of different ways I tried to accomplish solve the problem, I suspect that "%s" and "get_search_query" are not "firing" due to some conflict with the OpenAI code, or perhaps the pieces of code appear in the wrong order/location.
3. The two key files for you to look at, I think, are my child theme functions.php and search.php. The former contains all of the OpenAI-related code.
4. To help you understand the OpenAI code: the purpose is to facilitate best-in-class semantic search. I have a custom table in my wp database called gpt_search_embed. It contains vector embeddings of the title + content of each of my 1,862 custom posts. My functions.php file fetches the user's search query, sends it to the OpenAI API endpoint to retrieve its vector embedding, and then compares the query's embedding to the embeddings in the gpt_search_embed table. Essentially, I'm matching the semantic meaning of the user's query to my posts. This is really helpful for my use case because my posts consist of complex, technical legal rules full of jargon and terms of art, yet with OpenAI the user can ask plain language questions and find the relevant rules without keyword matching. OpenAI selects the top 75 results/posts, and then wordpress displays them on the search results page. The whole process if quite fast and the search results are like magic.
5. As an aside, I think someone (GeneratePress???) should partner with OpenAI to bring GPT-3 search to all websites, via a plugin or custom theme. I'd love to talk to you about that idea if you're interested. :)
6. Private info is below.
7. Sorry for such a long note. I really appreciate your help. I am a PHP and OpenAI beginner, and my fractional CTO is earning a masters so he's a bit distracted right now. Thanks. Leslie
2. Given the number of different ways I tried to accomplish solve the problem, I suspect that “%s” and “get_search_query” are not “firing” due to some conflict with the OpenAI code, or perhaps the pieces of code appear in the wrong order/location.
Yes this is one of the suspicions. Although you've mentioned that you did do var_dump( get_search_query ); and got it to return a value so it's a bit strange. Unless the var_dump you've done wasn't on the search page? that would make sense.
As for #3 & #4: we'll see what can be done once #2 is verified. Although I don't think digging into this won't be necessary if the main concern is just displaying the searched term.
5. As an aside, I think someone (GeneratePress???) should partner with OpenAI to bring GPT-3 search to all websites, via a plugin or custom theme. I’d love to talk to you about that idea if you’re interested. 🙂
While this is interesting, I'm not sure this will get prioritized as the dev team is occupied w/ releasing the merge of GenerateBlocks and WP Show Posts.
As for #6. please only provide the admin backend access to the WP admin dashboard. We won't be needing SFTP access. :)
Private info below. thx
Thanks.
I did a few change and while get_search_query() isn't working the query var $_GET['s'] works so we can use that.
Try this one on your template.
<header class="page-header">
<h1 class="page-title">
<?php
printf(
/* translators: 1: Search query name */
__( 'Search Results for: %s', 'generatepress' ),
'<span>' . $_GET['s'] . '</span>'
);
?>
</h1>
</header>
Let us know how it goes.
Yay, it worked! Thanks so much for persisting with helping me. As an aside, does anyone on the wordpress team do custom development? Thanks again. Leslie