Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 parantheses next to the title in the suggest form.

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

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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]);
    }
});
share|improve this answer
    
beautiful! Thanks –  marctain Jul 15 '12 at 23:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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