0

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.

2

The problem is that when there are no posts in the search query, WordPress doesn't set the post type in the global WP_Query (get_post_type() relies on that). So, when there are no search results, get_post_type() will return false and all of your custom template logic will be skipped.

What you can do is also consider looking at the request parameters and check if the post type is there and the right value. Also please note that you are not handling 404 for your custom post type (is_404()). Here is how your function would look with both cases taken into account:

function wikicon_include_templates( $template_path ) {
    if ( 'wikicon_recurso' === get_post_type() || 'wikicon_recurso' === get_query_var('post_type') ) {
        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() || is_404() ) {
            $template_path = plugin_dir_path( __FILE__ ) . '/archive-wikicon_recurso.php';
        }
    }

    return $template_path;
}
add_filter( 'template_include', 'wikicon_include_templates', 999, 1 );

A couple of tips:

  • I added a prefix to your function as it is good practice to do so (include_template_function is quite a common name);
  • there is no point in including _function in your function name :) ;
  • if want to make sure that no other plugin or theme will filter the templates and override your logic (and have you banging your head against the wall wondering why it's not working), you should use a big priority number, like 999, to make sure your logic executes last.

Let me know if this does the trick.

  • It worked! Thank you. I was trying to do something similar, figured out post was not being set but did not find how to do this. Going forward now on the plugin development, thank you very much. – Dani Aguado Jan 31 '18 at 9:12
0

Not a very indepth answer, and I'm not sure if this would work for your specific plugin scenario, but it seems to me that rather than trying to redirect to a different destination when there are no results, why don't you just handle it at the resulting destination? For instance, why not observe whether there are any posts (results) using have_posts and, if not, then provide the user a means of searching the wiki again?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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