0

This is my codes for search posts by ajax:

PHP:

public function search_posts(){
    try {
        $data = $_POST['content'];
        $data = wp_parse_args( $data, array(
            'post_type'     => 'post',
            'post_status'   => 'publish',
        ) );

        if ($data['s']){
            global $theme_query;
            $theme_query['s'] = explode(' ', $data['s']);
        }

        $query = new WP_Query($data);

        $data  = array();

        foreach ($query->posts as $search_post) {
            $search_post            = get_post($search_post);
            $search_post->permalink = get_permalink($search_post->ID);

            $data[] = $search_post;
        }

        $search_term = $_POST['content']['s'];

        $resp = array(
            'success'   => true,
            'data'      => array(
                'posts'     => $data,
                'total'         => $query->found_posts,
                'count'         => $query->post_count,
                'pages'         => $query->max_num_pages,
                'search_term'   => $search_term
            )
        );
    } catch (Exception $e) {
        $resp = array(
            'success' => false
        );
    }
    wp_send_json($resp);
}

HTML AJAX Template:

<script type="text/template" id="search_preview">
<# _.each(posts, function(search_post){ #>
<div class="item-search">
    <a href="{{= search_post.permalink }}">
        {{= search_post.post_title.replace( search_term, '<p>' + search_term + "</p>" ) }}
    </a>
</div>
<# }); #>

I successfully make ajax search for posts it's working fine now I need to include the search for users and tags in the search results like Facebook can search tags and users and pages at the same time.

I can search for users only or for tags only but I failed to merge all the results.

Anyone can help me on this?

  • Where is the code that you've tried to use to search for users and tags and merge the results? – janh Oct 7 '17 at 17:14
  • @janh2 Sorry I deleted it but I can explain what I did, I use the same PHP code but changing the way of it to can work with wp_user_query, I can make a single search system for users or tags or posts but I can't merge the results and that's sure because I use different query for each one. I hope you understand me. – Trello Oct 7 '17 at 17:31
  • The clean way is to build a search index for that, but quick and easy for smaller sites: take an array, do your three searches separately, push results onto that array like array_push($allresults, array("type" => "search", "url" => "...", "title" => "...")); etc. – janh Oct 7 '17 at 17:43
  • @janh2 Thank you but can you give me an example of can build a search index? Can you help me on this? Because I have a huge amount of users. – Trello Oct 7 '17 at 17:46
  • Look at the relevanssi search plugin for an example that you can probably expand to fit your needs. – janh Oct 7 '17 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.