Search code examples
ajaxwordpresssearchbootstrap-typeahead

WordPress multiple Ajax Search URLs


I'm using typeahead to enhance the search on my woocommerce website. Therefore I'd like to split products and content results for different FE-Display.

Im using the wp-admin/admin-ajax.php?action=ajax_search&fn=get_ajax_search&terms=[searchterm] to receive the results. Im trying to add something like "&posttype=post" or "&posttype=product" or "&posttype=page" to get different results.

But it seems, as it has no effect on the results. I can't limit the searchresults posttypes on the input field as I need all of these 3 resulttypes (and maybe more). Is there any chance to get different ajax-search URLs limited to the posttype?

Thank you in advance


Solution

  • thanks to @Bijal Patel I found the solution:

    I've added a new action "shop_search" and made up several different search queries which can be called using &pts=posttype so I can go for different searches like ?action=shop_search&pts=product&terms=test in functions

    add_action( 'wp_ajax_nopriv_shop_search', 'shop_search' );
    add_action( 'wp_ajax_shop_search', 'shop_search' );
    

    and then just like

    function shop_search() {
    if ( isset( $_REQUEST['pts'] ) && 'product' == $_REQUEST['pts'] ) { //pts = post type search // looking for Posttype = product
        $product_query = new WP_Query( array(
            's' => $_REQUEST['terms'],
            'post_type' => 'product', // Posttype goes here
            //'posts_per_page' => 10,
            'no_found_rows' => true,
        ) );
    
        $presults = array( );
    
        if ( $product_query->get_posts() ) {
    
            foreach ( $product_query->get_posts() as $the_post ) {
    
                Do STUFF 
    
                $presults[] = array(
                    'lorem' => $ipsum,
                    'foo' => $bar,
                );
            }
    
        } else {
            $presults[] = __( 'Sorry. No results match your search.', 'wp-typeahead' );
        }
    
        wp_reset_postdata();
        echo json_encode( $presults );
    }
    

    can be endless like...

    if ( isset( $_REQUEST['pts'] ) && 'post' == $_REQUEST['pts'] ) { //pts = post type search // Posttype = post
        $product_query = new WP_Query( array(
            's' => $_REQUEST['terms'],
            'post_type' => 'post', // posttype goes here
            //'posts_per_page' => 10,
            'no_found_rows' => true,
        ) );
    
       $results = array( );
    // see above for what to do next
    // And Close it all afterwards
        die(); }
    

    For all the posttypes I have to query, this may not be the fastest solution. If you just have to seperate between 2 or 3 posttypes, this will be it, more or less. Call the searchresults using: mysite.com/wp-admin/admin-ajax.php?action=shop_search&pts=product&terms=test

    So if anyone has an idea how to speed things up, please tell me! Thank you. And a special Thank you to Bijal - you made my weekend!