Want to redirect if search query match exact title of any post (1 post)

  1. mauriya
    Posted 4 years ago #

    I am using WordPress 3.3.1 and new to this system. I want to redirect the search query to the post if query exactly matches post title of some post.
    I also want it to run before any search query function and hooks or filter. I have made some changes in query.php in include folder

    $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}'))";

    and wrote some plugin function like this:

    add_action('template_redirect', 'single_result');
    function single_result() {
        if (is_search()) {
            global $wp_query;
            if ($wp_query->post_count == 1) {
                wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );

    I have also some function that need to run just after this search query and has "get_header" hooks. So far the search query match and redirect is not matching and returning as aspected. It most of the time slips out and don't redirect perfectly. I want search and redirect to be very perfect.
    Any help will be highly appreciated and thankful. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic