Forums

SearchAutocomplete
[PATCH] Support custom post types (4 posts)

  1. Daedalon
    Member
    Posted 9 months ago #

    To add support for custom post types, open includes/tags.php and change line 70 from

    $titles = $wpdb->get_results("SELECT post_title As name, ID as post_id, guid AS url, 1 cnt FROM ".$wpdb->prefix."posts t WHERE post_status='publish' and (post_type='post' OR post_type='page') and post_date < NOW() and post_title LIKE '%".mysql_real_escape_string($term)."%' ORDER BY post_title");

    to

    $titles = $wpdb->get_results("SELECT post_title As name, ID as post_id, guid AS url, 1 cnt FROM ".$wpdb->prefix."posts t WHERE post_status='publish' and post_date < NOW() and post_title LIKE '%".mysql_real_escape_string($term)."%' ORDER BY post_title");

    In other words, just remove the check for post_type.

    http://wordpress.org/extend/plugins/search-autocomplete/

  2. Daedalon
    Member
    Posted 9 months ago #

    To limit the amount of results returned, add LIMIT 10 before the last closing ", where 10 is the maximum amount of results you want to display. The resulting line 70 will then look like this:

    $titles = $wpdb->get_results("SELECT post_title As name, ID as post_id, guid AS url, 1 cnt FROM ".$wpdb->prefix."posts t WHERE post_status='publish' and post_date < NOW() and post_title LIKE '%".mysql_real_escape_string($term)."%' ORDER BY post_title LIMIT 7");

  3. Daedalon
    Member
    Posted 9 months ago #

    To roughly display posts of certain type before others, add post_type ASC, or post_type DESC, in between ORDER BY and post_title in the end. This displays the results grouped in alphabetical or reverse alphabetical order of the post type. To display eg. events before locations or pages before posts, use the first option (ASC), or to do it the other way around, the second.

    Here's an example line 70 with corrections in SQL capitalizations for improved readability and an unused query part (guid AS url) removed for performance:

    $titles = $wpdb->get_results("SELECT post_title AS name, ID AS post_id, post_type, 1 cnt FROM ".$wpdb->prefix."posts t WHERE post_status='publish' AND post_date < NOW() AND post_title LIKE '%".mysql_real_escape_string($term)."%' ORDER BY post_type DESC,post_title LIMIT 10");

  4. Daedalon
    Member
    Posted 9 months ago #

    To add qTranslate support, change includes/tags.php line 72 from

    $row_array['label'] = $word->name;

    to

    $row_array['label'] = __($word->name);

    Despite displaying the titles in the site's default language regardless of the user's language setting, this is a considerable improvement over the previously shown <!--:en-->Post Name<!--:--><!--:fi-->Artikkelin nimi<!--:--> jumble.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.