0

I am using wordpress. What I am doing is, that when the user enters anything then it will search from the post and will show the result.

I have added static content $_REQUEST['keyword']="team"; just for an example, Every time I get it No search found.

Any idea what is the issue?

function universalSearch( $atts ) {
    global $post;
    
    $_REQUEST['keyword'] = "leanding"; // it's just an example

    if ( ! empty( $_REQUEST['keyword'] ) ) {
        $args = array(
            'post_type' => 'post',
            's' => $_REQUEST['keyword'],
            'posts_per_page' => 5,
            'post_status' => 'publish',
        );
    }

    $result = new WP_Query( $args );
    
    if ( $result->have_posts() ) {
        while ( $result->have_posts() ) {
            $result->the_post(); 
            $tid = $result->ID;
            echo '<p><a href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a></p>';

        }
    } else {
        echo '<p><a href="javascript:void(0)">No search found.</a></p>';
    }
    
    wp_die();
}

I have check the wp query I am getting like this

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '{a30a32c58212e0e69da1fe713200b690a215277638267d3fc914587e000d21db}leanding{a30a32c58212e0e69da1fe713200b690a215277638267d3fc914587e000d21db}') OR (wp_posts.post_excerpt LIKE '{a30a32c58212e0e69da1fe713200b690a215277638267d3fc914587e000d21db}leanding{a30a32c58212e0e69da1fe713200b690a215277638267d3fc914587e000d21db}') OR (wp_posts.post_content LIKE '{a30a32c58212e0e69da1fe713200b690a215277638267d3fc914587e000d21db}leanding{a30a32c58212e0e69da1fe713200b690a215277638267d3fc914587e000d21db}'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_title LIKE
6
  • Where are the occurrences of team in your Posts?
    – Caleb
    Commented Jul 30 at 18:22
  • @Caleb, Team is just an example. $_REQUEST['keyword'] can be anything. Commented Jul 31 at 1:06
  • Do you have any posts which has team in their title? if yes then it should come in results. Commented Jul 31 at 9:12
  • @NarendraSishodiya, Yes, I have tried that, I am getting the value Commented Aug 1 at 4:21
  • Where are you calling this function from? Is there a reason you need to use a custom search function rather than WP's built-in search functionality?
    – WebElaine
    Commented Aug 1 at 18:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.