Results 1 to 7 of 7

Thread: multiple drop-down search form using custom fields and custom taxonomies [part 1]

  1. #1
    Thread Starter
    Amateur davidhaz's Avatar
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    0
    Thanked 8 Times in 6 Posts

    multiple drop-down search form using custom fields and custom taxonomies [part 1]

    in my website ( JeVendsMonPC.com | Vendez et achetez vos équipements informatiques en toute simplicité.JeVendsMonPC.com ) i've made several changes to the original classipress script, using a child theme to keep the original script untouched. The mods include searching on multiple taxonomies, adding custom fields, building an image gallery...
    in this tutorial i will expose how i implemented the multiple drop-down search form that you can see in the front page of the site. the form was accomplished using custom fields and custom taxonomies as i will be explaining in the following sections.

    MOD #1: building the multiple drop-down search box
    --------------------------------------------------

    1) Every single drop-down in that search box is built upon a specific taxonomies (that is a specific ads classification criterium, such as 'brand', 'color', size', 'location'...). So what you need to do first is create all the taxnomies according to which you want to order (classify) your ads. This how i did it for the 'location' drop-down box (the process is exactly the same for any other taxonomy ):
    1- open your functions.php
    2- paste in the following code:

    ////////////////////locations//////////////////////////////
    add_action( 'init', 'create_locations' );
    function create_locations() {
    $labels = array(
    'name' => _x( 'Ad Locations', 'taxonomy general name' ),
    'singular_name' => _x( 'Location', 'taxonomy singular name' ),
    'search_items' => __( 'Search Locations' ),
    'all_items' => __( 'All Locations' ),
    'edit_item' => __( 'Edit Location' ),
    'update_item' => __( 'Update Location' ),
    'add_new_item' => __( 'Add New Location' ),
    'new_item_name' => __( 'New Location Name' ),
    );

    register_taxonomy('ad_location','ad_listing',array (
    'hierarchical' => false,
    'labels' => $labels
    ));
    }
    ////////////////////////////////////////////////////////////

    for any other taxonomy you wanna create all you need to do is replace 'location' with your taxonomy's name. Please be advised that taxonomy names are case-sensitive.
    3- there it is! you have created your 'custom taxonomy'. to check this, go to your WP admin page and see under the "Ads" tab. you will notice a new "Ad Locations" link.
    4- the next step is to add values to this 'locations' taxonomy. you could do so inside the "Ad Locations" section. however, doing so that way you'll still need to "link" your posted ads to the corresponding location. otherwise, you will have ad locations with no ads linked to!.
    how to do that will be covered in my next post, very soon.
    ////////////////////////////////////////////////////////////////////////////////////////////////////

    Now that we have our taxonomies created, we need to 'fill' them with 'terms' from our ad posting forms. Ok, let me elaborate a little bit on this:
    in your 'post a new ad' form you have many fields, some of which you want to use as 'taxonomies' to order / classify your ads. say you have a 'location' field in your form. now that you have created the 'locations' taxonomy (see above), once you submit your ad posting the value of the 'location' field to be systematically linked to your 'locations' field so that it would sho in the 'locations' drop-down box:
    1- open your step-functions.php file (located in your themes -> classipress -> includes -> forms folder)
    2- locate this line " //set the custom post type tags
    wp_set_post_terms($post_id, $new_tags, 'ad_tag', false );"
    3- right below this line, paste the following:
    wp_set_post_terms( $post_id, $advals['cp_location'], 'ad_location' );
    this will automatically 'fill' the 'locations' taxonomy with the value of your 'location' field on post form submission. make sure to replace the 'cp_location' (which is the field name) with your own field name corresponding to the taxonomy you want to 'link' to.
    4- now, on form submission, the value in the 'location' field will be available for the 'location' drop-down box.

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    next, paste the following function into your 'functions.php' file:
    ////////////////////////////// Build my custom drop down menu ///////////////////////////////////

    if (!function_exists('my_menu_drop_down')) {
    function my_menu_drop_down($my_taxonomy, $hierarchy, $hidempty, $cols = 3, $subs = 0) {
    global $wpdb;
    global $my_menu;

    // get any existing copy of our transient data
    if (false === ($my_menu = get_transient('my_menu'))) {

    // put all options into vars so we don't have to waste resources by calling them each time from within the loops
    $cp_cat_parent_count = get_option('cp_cat_parent_count');
    $cp_cat_child_count = get_option('cp_cat_child_count');
    $cp_cat_hide_empty = get_option('cp_cat_hide_empty');
    $cp_cat_orderby = get_option('cp_cat_orderby');

    // get all cats for the taxonomy
    $my_menu_terms = get_terms($my_taxonomy, 'hide_empty='.$hidempty.'&hierarchical='.$hierarch y.'&pad_counts=1&show_count=true&orderby=name&orde r=ASC');

    //remove all sub cats from the array
    foreach ($my_menu_terms as $key => $value)
    if ($value->parent != 0) unset($my_menu_terms[$key]);


    $i = 0;
    $menu_cols = $cols; // change this to add/remove columns
    $total_main_parent_cats = count($my_menu_terms); // total number of parent cats
    $items_per_col = round($total_main_parent_cats / $menu_cols); // items per column

    // loop through all the sub
    foreach($my_menu_terms as $my_term) :

    if (($i == 0) || ($i == $items_per_col) || ($i == ($items_per_col * 2)) || ($i == ($items_per_col * 3)) ) {
    if ($i == 0) $first = ' first';
    $my_menu .= '<div class="catcol '. $first .'">';
    }

    // only show the total count if option is set
    if($cp_cat_parent_count == 1)
    $show_count = '('. $my_term->count .')';

    $my_menu .= '<ul>';
    $my_menu .= '<li class="maincat cat-item-'. $my_term->term_id .'"><a href="'. get_term_link($my_term, $my_taxonomy) .'" title="'. esc_attr($my_term->description) .'">'. $my_term->name .'</a> '.$show_count.'</li>';


    // don't show any sub cats

    if (is_taxonomy_hierarchical( $my_taxonomy )):
    if($subs <> 999) :


    // now get all the sub cats based off the parent cat id

    $subcats = @wp_list_categories('orderby='.$cp_cat_orderby.'&t axonomy='.$my_taxonomy.'&order=asc&show_count='.$c p_cat_child_count.'&hierarchical=0&pad_counts=1&us e_desc_for_title=1&hide_empty=1&depth=1&title_li=& echo=0&number='.$subs.'&child_of='.$my_term->term_id.'&show_option_none='.get_option('cp_cat_s trip_nocatstext'));

    // print out all the subcats for the current parent cat

    $my_menu .= $subcats;


    endif;
    endif;


    $my_menu .= '</ul>';

    if (($i == ($items_per_col - 1)) || ($i == (($items_per_col * 2) - 1)) || ($i == (($items_per_col * 3) - 1)) || ($i == ($total_main_parent_cats - 1)))
    $my_menu .= '</div><!-- /catcol -->';

    $i++;

    endforeach;

    return $my_menu;

    // set transient
    set_transient('my_menu', $my_menu, get_option('cp_cache_expires'));

    } else {

    // must already be transient data so use that
    return get_transient('my_menu');

    }

    }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////

    Ok, so what's going on here ? well, this is nothing but a tweaked version of appthemes' function called 'cp_cat_menu_drop_down' found in classipress -> includes -> theme-functions file. you don't need to change anything in the theme-functions file. just paste the above function into your functions.php file.
    Now, you're almost done! you have your taxonomy, which gets filled with the terms submitted through your ad posting form and you have a function that builds the drop-down box containing the values of your taxonomy. next, we'll build the actual thing using the drop-down box.

  2. The Following User Says Thank You to davidhaz For This Useful Post:

    davidcvd (June 7th, 2011)

  3. #2
    Veteran barukar's Avatar
    Join Date
    Sep 2010
    Location
    Brasil, São Paulo, SP
    Posts
    6,785
    Thanks
    186
    Thanked 742 Times in 623 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!
    -------------------------------------------------------------------------------------------
    Projects: ClassiNoiva - Classimóveis - vocênoenem - i50 - Clube DETRAN

  4. #3
    Thread Starter
    Amateur davidhaz's Avatar
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    0
    Thanked 8 Times in 6 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  5. The Following User Says Thank You to davidhaz For This Useful Post:

    davidcvd (June 7th, 2011)

  6. #4
    Newbie antonyedgar's Avatar
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  7. #5
    jomarkosabel's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    40,713
    Thanks
    166
    Thanked 3,390 Times in 3,261 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!
    Please help our moderating team work more efficiently by not sending us support questions via PM. You can read more about how AppThemes support works here. However, you can send a PM to follow up and remind me if I missed your support request/thread.

    Thank you and have a nice day.

  8. #6
    Thread Starter
    Amateur davidhaz's Avatar
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    0
    Thanked 8 Times in 6 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

  9. #7
    Thread Starter
    Amateur davidhaz's Avatar
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    0
    Thanked 8 Times in 6 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [TUTORIAL] Part ONE -- changing the way Custom Fields are named 3.05.1
    By vienna in forum ClassiPress General Discussion
    Replies: 2
    Last Post: November 23rd, 2010, 03:00 PM
  2. Custom Taxonomies and custom post types in Classipress
    By carlos takemura in forum ClassiPress General Discussion
    Replies: 1
    Last Post: July 8th, 2010, 03:26 PM