• Hi,

    I was wondering if anyone can help on this. I have installed the Multiple Category Search Widget plugin. I was looking to see if it was possible to add some code to give the ability to generate a dropdown based on a selection of a particular category.

    Currently, the code generates dropdowns of subcategories for each category. What I wanted to do is when a user selects a country (each country would be a seperate parent category) then the corresponding category dropdown of subcategories would generate. I presume this would involve some Javascript somewhere in the below code but I am a bit lost on how to go about this.

    Here is the code so far with some small edits:

    ———————————————————————–

    */

    function wpmm_load_category()
    {
    if (!empty($_POST[‘wpmm’]))
    {
    $cats = $_POST[‘wpmm’];
    $count = 0;
    $sql = array();
    foreach ($cats as $cat)
    {
    if ($cat == 0)
    unset($cats[$count]);
    else
    array_push($sql, $cat);
    $count++;
    }

    query_posts(array(‘category__’.$_POST[‘mmctype’] => $sql));
    }
    }

    function widget_wpmm($args)

    {
    extract($args);
    global $wpdb;

    $children = $wpdb->get_results(‘SELECT tx.term_id, tx.parent, t.name
    FROM ‘.$wpdb->term_taxonomy.’ as tx, ‘.$wpdb->terms.’ as t WHERE tx.taxonomy = “category”
    AND t.term_id = tx.term_id AND tx.parent != 0
    ORDER BY tx.parent, t.name ASC’, ARRAY_A);

    $parents = $wpdb->get_results(‘SELECT tx.term_id, t.name
    FROM ‘.$wpdb->term_taxonomy.’ as tx, ‘.$wpdb->terms.’ as t WHERE tx.taxonomy = “category”
    AND t.term_id = tx.term_id AND tx.parent = 0 AND t.name != “Uncategorized”

    ORDER BY t.name ASC’, ARRAY_A);

    echo $before_widget;
    echo $before_title . ‘<center> Search by Item and Location </center>’ . $after_title .’

    <center><form action=”” method=”post” id=”wpmm”>

    ‘;

    foreach ($parents as $p)
    {
    echo ‘
    <select name=”wpmm[]”>
    <option value=”0″>’.apply_filters(‘single_cat_title’, stripslashes(str_replace(‘”‘, ”, $p[‘name’]))).'</option>’;

    foreach ($children as $c)
    {
    $selected = ”;

    if (!empty($_POST[‘wpmm’]))
    {
    foreach ($_POST[‘wpmm’] as $cat)
    {
    if ($cat == $c[‘term_id’])
    $selected = ‘selected=”selected”‘;
    }
    }

    if ($p[‘term_id’] == $c[‘parent’])
    echo ‘<option ‘.$selected.’ value=”‘.$c[‘term_id’].'”> ‘.apply_filters(‘single_cat_title’, stripslashes(str_replace(‘”‘, ”, $c[‘name’]))).'</option>’;
    }

    echo ‘</select>’;
    }

    $checked1 = ”;
    $checked2 = ”;

    if ($_POST[‘mmctype’] == ‘and’)
    $checked2 = ‘checked=”checked”‘;
    else if ($_POST[‘mmctype’] == ‘in’)
    $checked1 = ‘checked=”checked”‘;
    else
    $checked2 = ‘checked=”checked”‘;

    echo ‘Any <input type=”radio” name=”mmctype” value=”in” ‘.$checked1.’> All <input type=”radio” name=”mmctype” value=”and” ‘.$checked2.’>
    <input type=”submit” value=”GO”></form></center>’.$after_widget;
    }

    function wpmm_widgets()
    {

    register_sidebar_widget(‘Multi-Category’, ‘widget_wpmm’);
    }

    add_action(‘init’,’wpmm_load_category’);
    add_action(‘plugins_loaded’, ‘wpmm_widgets’);

    ?>

  • The topic ‘Multiple Category Search – Advanced Hack’ is closed to new replies.