Site logo

Archived topic

creating a custom template for search results generated by external API

4 replies · Started by Leslie on August 26, 2021

Viewing posts 1–5 of 5

Hi, I am replacing native wp search with search results generated by an external API that uses sophisticated NLP. I am struggling with how to create a custom template for displaying these non-wordpress-native search results. For displaying archives of my custom post type xyz, I have leared how to create content-xyz.php and archive-xyz.php files, in my child theme folder. I have also inserted searchforms in custom locations, with custom parameters like post type, using your hook elements (which I LOVE!!). But, I am stumped as to how to display search results that are not generated by the native wordpress search. The data I'll obtain from the external API will include an array of ids and I'll put those into a var like $externalapi_ids and then do something like $query = new WP_Query( array( 'post__in' => $externalapi_ids)). To display this array of posts, I believe I need a content-abc.php file as well as a template file that contains something like this:
while ( have_posts() ) : the_post(); get_template_part( 'content', 'abc' ).
But no matter how much documentation I read, I can't seem to figure out whether the template file should be archive-abc.php or search-abc.php or custom-abc.php or even index-abc.php. For displaying my custom posts xyz, I knew that wordpress would look first in my child theme folder for content-xyz and archive-xyz. But I don't know how to tell wordpress what files to look for in my situation. Any guidance you can provide is greatly appreciated. I know that this is not purely a theme question but more generally a wordpress development question that involves the use of theme template files...But I hope you can perhaps share some ideas regardless. Thanks very much. Leslie

Thanks David, this is helpful. However, I am still confused about where to put the code. I have asked the author of the final snippet why he said "no child theme is necessary." My understanding is that functions.php and search.php (and other theme files) should not be altered directly; instead, a child theme should be created, or a custom plugin. None of the built-in GP hook elements looks right for this. Do you suggest altering search.php? What would happen when GP gets updated? Thanks again.

David, I heard back from Acela. He used the code snippets plugin. I had forgotten about that option, since I have a child theme functions.php already. FWIW, search is front and centre nowadays so I agree with Acela that GP would do well to make its documentation very clear on how to replace the default WP search. You could say that this isn't a theme-specific issue, but ironically it takes hours of research to learn that it isn't a theme issue and can be solved outside the theme files. In your documentation section "How to add php" you could provide an example like "Here's how you can add php to replace default WP search results with custom search results". Thanks David.

Glad to hear you got it working.
And thanks for the feedback, we'll see if we can write something up that covers most bases!

This archived topic is closed to new replies.