Title: [Patch] Smart404, sane post redirection behavior &amp; sticky posts
Last modified: August 20, 2016

---

# [Patch] Smart404, sane post redirection behavior & sticky posts

 *  [belg4mit](https://wordpress.org/support/users/belg4mit/)
 * (@belg4mit)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/patch-smart404-sane-post-redirection-behavior-sticky-posts/)
 * This patch tries to improve the abandoned Smart404 plugin. The main change is
   to not redirect if a post or page matches the request unless only one post or
   page matches. This is different from the default behavior where if only one post
   matches but many pages match, the post will still be redirected to. Other tweaks
   are to remove unnecessary exit commands, and to try to to exclude sticky posts
   from the recommendation list (although this seems to fail in WP3.5).
 *     ```
       --- /tmp/smart-404/smart404.php	2010-09-14 14:22:33.000000000 -0500
       +++ smart404.php	2012-12-20 09:53:26.000000000 -0600
       @@ -3,7 +3,7 @@
        Plugin Name: Smart 404
        Plugin URI: http://atastypixel.com/blog/wordpress/plugins/smart-404/
        Description: Rescue your viewers from site errors!  When content cannot be found, Smart 404 will use the current URL to attempt to find matching content, and redirect to it automatically. Smart 404 also supplies template tags which provide a list of suggestions, for use on a 404.php template page if matching content can't be immediately discovered.
       -Version: 0.5
       +Version: 0.5 PATCHED: don't redirect when one page matches if multiple posts have also matched, try to avoid sticky posts in recommendations
        Author: Michael Tyson
        Author URI: http://atastypixel.com/blog/
        */
       @@ -75,10 +75,9 @@
                switch ( $group ) {
                    case "posts":
                        // Search for posts with exact name, redirect if one found
       -        	    $posts = get_posts( array( "name" => $search, "post_type" => "post" ) );
       +	      $posts = get_posts( array( "name" => $search, "post_type" => "post" ) );
                 		if ( count( $posts ) == 1 ) {
                   			wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 );
       -           			exit();
                   		}
                        break;
   
       @@ -87,7 +86,6 @@
                        $posts = get_posts( array( "name" => $search, "post_type" => "page" ) );
                 		if ( count( $posts ) == 1 ) {
                   			wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 );
       -           			exit();
                   		}
                		break;
   
       @@ -96,7 +94,6 @@
                		$tags = get_tags( array ( "name__like" => $search ) );
                		if ( count($tags) == 1) {
                			wp_redirect(get_tag_link($tags[0]->term_id) . $get_params, 301);
       -        			exit();
                		}
                		break;
   
       @@ -105,7 +102,6 @@
                		$categories = get_categories( array ( "name__like" => $search ) );
                		if ( count($categories) == 1) {
                			wp_redirect(get_category_link($categories[0]->term_id) . $get_params, 301);
       -        			exit();
                		}
                		break;
                }
       @@ -113,28 +109,22 @@
   
            // Now perform general search
            foreach ( $search_groups as $group ) {
       +      #We could do away with this switch if the stored option was singular
                switch ( $group ) {
                    case "posts":
                        $posts = smart404_search($search, "post");
       -         		if ( count( $posts ) == 1 ) {
       -           			wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 );
       -           			exit();
       -           		}
       -
       -           		$GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts );
       +           	$GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts );
                        break;
   
                    case "pages":
                        $posts = smart404_search($search, "page");
       -         		if ( count( $posts ) == 1 ) {
       -           			wp_redirect( get_permalink( $posts[0]->ID ) . $get_params, 301 );
       -           			exit();
       -           		}
       -
       -           		$GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts );
       -        		break;
       +           	$GLOBALS["__smart404"]["suggestions"] = array_merge ( (array)$GLOBALS["__smart404"]["suggestions"], $posts );
       +        	break;
                }
            }
       +    if( count($GLOBALS["__smart404"]["suggestions"]) == 1 ){
       +      wp_redirect( get_permalink( $GLOBALS["__smart404"]["suggestions"][0] ) . $get_params, 301 );
       +    }
        }
   
       @@ -149,7 +139,15 @@
         */
        function smart404_search($search, $type) {
            $search_words = trim(preg_replace( "@[_-]@", " ", $search));
       -	$posts = get_posts( array( "s" => $search_words, "post_type" => $type ) );
       +	$posts = get_posts( array( "s" => $search_words,
       +				   "post_type" => $type,
       +
       +				   #Alas, neither of these seem to work in 3.5
       +				   'ignore_sticky_posts' => true,
       +#				   'post__not_in' => get_option('sticky_posts')
       +				   )
       +			    );
       +
        	if ( count( $posts ) > 1 ) {
        	    // See if search phrase exists in title, and prioritise any single match
        	    $titlematches = array();
       ```
   
 * A work-around for the failure to exclude sticky posts is to do so in your 404
   template e.g;
 *     ```
       <?php if (smart404_loop()) : ?>
                               <?php while (have_posts()) : the_post(); ?>
                                  <?php #Work-around get_posts ignoring post__not_in and ignore_sticky_posts
                                       if( ! is_sticky() ){ ?>
                                       <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
                                       <small><?php the_excerpt(); ?></small>
                                  <?php }
                                     endwhile; ?>
                      <?php endif; ?>
                      </div>
   
          <?php } ?>
       ```
   
 * [http://wordpress.org/extend/plugins/smart-404/](http://wordpress.org/extend/plugins/smart-404/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [cadbloke](https://wordpress.org/support/users/cadbloke/)
 * (@cadbloke)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/patch-smart404-sane-post-redirection-behavior-sticky-posts/#post-3295585)
 * Have you ever tried to apply a patch in Windows?
 * Thanks for patching this but it would be much more convenient to just post the
   whole, edited file.
 * cheers
    Ewen
 *  Thread Starter [belg4mit](https://wordpress.org/support/users/belg4mit/)
 * (@belg4mit)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/patch-smart404-sane-post-redirection-behavior-sticky-posts/#post-3295587)
 * > Have you ever tried to apply a patch in Windows?
   > Thanks for patching this but it would be much more convenient to just post 
   > the whole, edited file.
 * Q1) Yes. Q2) No, it wouldn’t. Posting the whole file wastes space, and makes 
   it difficult to see what the changes are. With a unified diff, even if you are
   on an ill-equipped OS (Cygwin is your friend), you can still make the changes
   by hand, since they are clearly visible. A simple search of your favorite search
   engine will also reveal any number of tools for making and applying patches on
   Windows.
 *  [cadbloke](https://wordpress.org/support/users/cadbloke/)
 * (@cadbloke)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/patch-smart404-sane-post-redirection-behavior-sticky-posts/#post-3295591)
 * Hi belg4mit
 * I tried patching with TortoiseMerge but it never got over the whitespace differences.
   Probably a copy-paste thing. The great debate of patch files is a minor thing–
   thanks for posting the patch.
 * cheers.
    Ewen
 *  [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * (@airdrummer)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/patch-smart404-sane-post-redirection-behavior-sticky-posts/#post-3295595)
 * i’ve also coded some improvements:
    - redirect to 1st match found, otherwise show all matches
    - redirect to exact match if found
    - try to match on whole uri; default is tail only
    - try walking up uri
    - Debug: >0 to send debug to error log (1-5)
 * i’ve put it up as [source](http://wibles.webhop.net/toms/stuff/smart404_php.txt)&
   [patch](http://wibles.webhop.net/toms/stuff/smart404.patch)
 *  [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * (@airdrummer)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/patch-smart404-sane-post-redirection-behavior-sticky-posts/#post-3295596)
 * i 4got 2 mention: the list of improvements is from the settings interface…all
   can be enabled/disabled/set.
 * but i’ve never had any success w/Ignored patterns…

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘[Patch] Smart404, sane post redirection behavior & sticky posts’ is closed
to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/smart-404.svg)
 * [Smart 404](https://wordpress.org/plugins/smart-404/)
 * [Support Threads](https://wordpress.org/support/plugin/smart-404/)
 * [Active Topics](https://wordpress.org/support/plugin/smart-404/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/smart-404/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/smart-404/reviews/)

## Tags

 * [redirect](https://wordpress.org/support/topic-tag/redirect/)

 * 5 replies
 * 3 participants
 * Last reply from: [airdrummer](https://wordpress.org/support/users/airdrummer/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/patch-smart404-sane-post-redirection-behavior-sticky-posts/#post-3295596)
 * Status: not resolved