PHP code example of amostajo / wordpress-search-typeahead

1. Go to this page and download the library: Download amostajo/wordpress-search-typeahead library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

amostajo / wordpress-search-typeahead example snippets


    'Amostajo\Wordpress\TypeaheadAddon\Typeahead',

add_filter( 'addon_typeahead_query', 'filter_query' );

function filter_query ($args) {
    // Array modifications
    $args['post_type'] = 'product';
    $args['posts_per_page'] = 10;

    // Array is expected in return
    return $args;
}

add_filter( 'addon_typeahead_post', 'filter_post' );

function filter_post ($post) {

    // Transformation of post into custom model
    $model = new MyModels\CustomPost();

    // Array is expected in return
    return $model->from_post( $post )->to_array();
}

add_filter( 'addon_typeahead_data', 'filter_data' );

function filter_data ($data, $args) {

    // Adding custom records in results
    if ( $args['post_type'] != 'product' ) {
        // Adding extra product
        $data[] = MyProduct::find(999)->to_array();
    }

    // Array is expected in return
    return $data;
}
html
<div class"search">
     typeahead_search() 
html
<div class"search">
     typeahead_search( [ 'post_type' => 'product' ] )