• Resolved warnerta

    (@warnerta)


    Hi, this may not be directly about your plugin, but before I get too deep into it, I want to ask if you know if this kind of functionality is possible with WordPress.

    You know how with MediaWiki, when you enter a search term that’s a match for a wiki page, you are taken directly to that page?

    As opposed to the normal WordPress behavior, which would give you a list of search results, hopefully with the page named identically to the search term at the top, followed by a bunch of other pages that include the search term.

    Do you know is that kind of MediaWiki-style search possible in WordPress? Is there a plugin that does it?

    Best regards, Tom

    https://www.ads-software.com/plugins/yada-wiki/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author dmccan

    (@dmccan)

    Hi warnerta,

    I looked around and found this that seems to work:

    function redir_title_match($a) {
      if (is_search()) {
        global $wp_query;
        $s_str = $wp_query->query_vars['s'];
        foreach ($wp_query->posts as $p) {
          if (strtolower($p->post_title) == strtolower($s_str)) {
            wp_safe_redirect(get_permalink($p->ID));
            exit();
          }
        }
      }
    }
    add_filter('template_redirect','redir_title_match');

    from here (I made it non-case sensitive):

    https://wordpress.stackexchange.com/questions/49208/want-to-redirect-if-search-query-match-exact-title-of-any-post

    If you have a child theme then you can add it to the child theme’s functions.php. If you do not have a child them (or it is way easier to do so) you can use a plugin called “Code Snippets” and add it there (which is what I did to test).

    I am going to mark this as resolved, but feel free to followup if I didn’t understand the question.

    Hope this helps,

    David

    Thread Starter warnerta

    (@warnerta)

    Thank you, that was very helpful of you. In a nutshell the answer to my question is: yes, Wiki-style search redirects to exact term-to-page matches can be done in WordPress, and there are multiple ways of doing it. But as of now, no ready-made plugin. So yes, mark this resolved. I just posted here because I figured you or your users might know more than I could find by searching the web.

    If and when I get something clean and working I’ll report back. I’m looking at another method that uses WP’s get_page_by_title() function. It’s explained here: https://trepmal.com/2011/04/21/redirect-when-search-query-is-an-exact-title-match/

    Best regards.

    Thread Starter warnerta

    (@warnerta)

    Hi again. I have tested out the alternative method I referred to and it works well. I think it’s better practice to use WordPress’ built-in get_page_by_title function than to code your own search loop. Also make for more compact code.

    This is revised and improved from the version I linked to, which starts off with the right idea but makes some mistakes. For example it’s very important to test for ‘publish’ status before redirecting, or else you could allow redirects to private or trash content.

    Note that the test for an exact match is not case sensitive.

    Also note that this function is hard-coded to redirect only when search terms exactly match the titles of posts of a custom post type called ‘wiki’. If you want to redirect to regular posts, pages or posts of another custom post type called for example ‘whatever’, replace ‘wiki’ as the third parameter of get_page_by_title() with ‘post’ or ‘page’ or ‘whatever’. To redirect to exact matches of multiple types of posts and/or pages, duplicate the core section of the code – everything inside the curly brackets of if (is_search()).

    Cheers and hope this is also useful to somebody besides me.

    add_action ( 'template_redirect', 'exact_match_redirect');
    
    function exact_match_redirect()
    {
    global $wp_query;
    if (is_search())
    	{
    	$matched_wiki_post = get_page_by_title ( get_search_query(), 'OBJECT', 'wiki' );
    	if ( !empty ($matched_wiki_post) )
    		{
    		if ( $matched_wiki_post->post_status == 'publish' )
    			{
    			wp_redirect( get_permalink ( $matched_wiki_post->ID ) );
                            exit();
    			}
    		}
    	}
    }
    Plugin Author dmccan

    (@dmccan)

    Awesome. Thank you for sharing.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Wiki-style direct page landing from search function?’ is closed to new replies.