Find similair wordpress posts from user input

im trying to show the closest matching wordpress post depending on user input. I have tried using levenstheins distance as you can see below but it doesn’t work. If anyone also know how to show the content of the posts that is selected it would be great.

The code I have this far is below. Thanks!

 $gallery_args = array(
                    'posts_per_page'   => -1,
                    'orderby'=> 'date',
                    'order'=> 'DESC',
                    'post_type'=> 'gallery',
                    'post_status'=> 'publish',
                    'suppress_filters' => true 
        );
        $posts_gallery = get_posts( $gallery_args ); 
        foreach($posts_gallery as $rows){
            $post_titles = $rows->post_title;
        }

       $user_input = //Users input string
       $shortest = -1;

    foreach ($post_titles as $post_title) {

    $lev = levenshtein($user_input, $post_title);

    // check for an exact match
    if ($lev == 0) {

        $closest = $post_title;
        $shortest = 0;

        break;
    }

    if ($lev <= $shortest || $shortest < 0) {
        $closest  = $post_title;
        $shortest = $lev;
    }
}

if ($shortest == 0) {
    echo "Exact match found: $closestn" . "<br />";
} else {
    echo  $closest;
}
}
} 

Related posts

1 comment

  1. Your code works, but the $user_input-variable is missing. With that variable set, and a manually set $post_titles-array it will give you the closest matching post_title.

    I’ve included a function (at top) to display the closest post.

    My testcode:

        <?php
    
       function display_closest_post($thePostID) {
         $post = get_post($thePostID);
         setup_postdata($post);
    
         // display the post here
         the_title();
         the_excerpt();
         the_post_thumbnail();
    
         wp_reset_postdata();
       }
    
        $post_titles = [
            "1" => "car",
            "2" => "bar",
        ];
    
              $user_input = 'cab';
               $shortest = -1;
    
            foreach ($post_titles as $post_title) {
    
            $lev = levenshtein($user_input, $post_title);
    
            // check for an exact match
            if ($lev == 0) {
    
                $closest = $post_title;
                $closestID = get_the_ID(); //Fetch the ID of the post
                $shortest = 0;
    
                break;
            }
    
            if ($lev <= $shortest || $shortest < 0) {
                $closest  = $post_title;
                $closestID = get_the_ID(); //Fetch the ID of the post
                $shortest = $lev;
            }
        }
    
        if ($shortest == 0) {
            echo "Exact match found: $closestn" . "<br />";
            //Shows the whole post
            display_closest_post($closestID);
        } else {
            echo  $closest;
            //Shows the whole post
            display_closest_post($closestID);
        }
    
        ?>
    

Comments are closed.