0

I have a custom post type of show and I am trying to create a search input where the following occurs:

  1. User can start typing a few letters and auto completed suggestions (based on the post title) populate.

  2. Next to (in same row as) the post title for each that populates based on the users typing, I would like to display info from the custom field show-date thus an example of a populated result would be: "Stack Show (Jan 12, 2018)"

  3. When user clicks one of the options, it automatically loads the page of that post as opposed to having to click a submit button etc.

I have researched all the posts on stack about this topic for the past few days and can not get anything working. Below is the most recent code I have been trying. I am only posting this code as what I have tried as it is the most recent method whereas older methods I have found on stack may not work now based on changes in WP. Please note that nothing populates at all when typing in the input box (I have seen some users say the problem they run into is it populates ALL posts...for me nothing happens at all).

functions.php

add_action('wp_enqueue_scripts', 'se_wp_enqueue_scripts');
function se_wp_enqueue_scripts() {
    wp_enqueue_script('suggest');
}

add_action('wp_header', 'se_wp_head');
function se_wp_head() {
?>
Search: <input id="se_search_element_id" type="text">
<script type="text/javascript">
var se_ajax_url = '<?php echo admin_url('/wp-admin/admin-ajax.php'); ?>';

jQuery(document).ready(function() {
    jQuery('#se_search_element_id').suggest(se_ajax_url + '?action=se_lookup', {minchars:1});
});
</script>

<?php
}

add_action('wp_ajax_se_lookup', 'se_lookup');
add_action('wp_ajax_nopriv_se_lookup', 'se_lookup');

function se_lookup() {
    global $wpdb;

    $search = $wpdb->esc_like($_REQUEST['q']);

    $query = 'SELECT ID,post_title FROM ' . $wpdb->posts . '
        WHERE post_title LIKE \'' . $search . '%\'
        AND post_type = \'show\'
        AND post_status = \'publish\'
        ORDER BY post_title ASC';
    $rows = $wpdb->get_results($query);
    foreach ($rows as $row) {
        $post_title = $row->post_title;
        $id = $row->ID;
        echo $post_title, "\n";
    }
    wp_die();
}

and here is my markup, not sure what could go wrong here but just in case...

<input id="se_search_element_id" type="text">

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.