I've been looking for the solution of this issue for a while but cannot find the correct answer.
I'm developing a custom Knowledge Base plugin for my site, as requested by my boss. The around plugins are not working for he want, so I need to create my own.
I've a Custom Post Type called wikicon_recurso
with it's own taxonomies, metadata and so on.
What I'm having trouble now is for the search function inside my own "Wiki". When someone clicks on "Wikicon" at the menu, it will redirect to the Archive Template, which I have modified so it shows a search form and the last posts added to the wiki.
When the search returns results, it redirects to my search-wikicon_recurso.php inside the plugin folder and shows the results as I want.
I'm having trouble to modify the template when no results are found. For example, I have a "test" resource, when I find test I'm redirected to the Search template, which is cool. But if I find Hello, as there is no results, I'm redirected to the default Wordpress/Theme no results page, where the search form is not taking the post_type parameter and is looking for everything.
How can I modify the destination of empty results to my Search Template, for example, so they can search again?
Actually on my plugin I'm redirecting to templates like this:
add_filter( 'template_include', 'include_template_function', 1 );
function include_template_function( $template_path ) {
if ( get_post_type() == 'wikicon_recurso' ) {
if ( is_singular() ) {
$template_path = plugin_dir_path( __FILE__ ) . '/single-wikicon_recurso.php';
}
elseif (is_search()) {
$template_path = plugin_dir_path( __FILE__ ) . '/search-wikicon_recurso.php';
}
elseif (is_archive()) {
$template_path = plugin_dir_path( __FILE__ ) . '/archive-wikicon_recurso.php';
}
}
return $template_path;
}
This is the search form I'm using from other answers I found around here. It's shown both on the archive-wikicon_recurso.php and search-wikicon_recurso.php:
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search query"/>
<input type="hidden" name="post_type" value="wikicon_recurso" />
<input type="submit" alt="Search" value="Search" />
</form>
I've tried multiple answers from this page and further searchs at Google but I cannot make any to work.
I have to say is my first time creating a plugin this big for Wordpress so starting to learn. Thanks in advance.