0

I'm using wordpress' built in suggest scripts to create an auto-suggest form based on custom post titles...

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

add_action('wp_head', 'se_wp_head'); function se_wp_head() { ?>

var se_ajax_url = ''; jQuery(document).ready(function() { jQuery('#stuff-input').suggest(se_ajax_url + '?action=se_lookup'); });

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

function se_lookup() { global $wpdb;

$search = like_escape($_REQUEST['q']); $query = 'SELECT ID,post_title FROM ' . $wpdb->posts . ' WHERE post_title LIKE \'' . $search . '%\' AND post_type = \'stuff\' AND post_status = \'publish\' ORDER BY post_title ASC'; foreach ($wpdb->get_results($query) as $row) { $post_title = $row->post_title; $id = $row->ID; $meta = get_post_meta($id, 'location', TRUE); echo $post_title . ' (' . $meta . ')' . "\n"; } die(); }

Currently, it displays meta-data associated with each post in parentheses next to the title in the suggest form.

The problem is when the suggested item is clicked, it adds the parentheses to the form, as well as the title.

I want the parentheses to be visible when the title is SUGGESTED, but not to appear in the form box when the suggested title is clicked.

1

Change the jQuery:

jQuery('#stuff-input').suggest(se_ajax_url + '?action=se_lookup', {
    onSelect: function() {
        thevalue = this.value;
        thevalue = thevalue.split(' (');
        jQuery('#stuff-input').val(thevalue[0]);
    }
});

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.