• Resolved jksastrology

    (@jksastrology)


    Hello,

    I have used BuddyPress Xprofile Custom Fields plugin so members need to select their height (in feet) using a drop down box.

    I want to use the bp profile search plugin to find members by a specified height range. However, I cannot find a way to do this.

    For example, I want members to have the ability to search members which have a height between 5″6′ and 6″. Does anyone know the best way of achieving this?

    Any help is much appreciated.

    Kind regards,

    Jamie

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Andrea Tarantini

    (@dontdream)

    Hello Jamie,

    Which field type are you using? And could you post the values that are in your drop-down box? Not necessarily all of them, just a few examples.

    Thread Starter jksastrology

    (@jksastrology)

    Hello Andrea,

    Thank you for your quick response.

    I am using “Drop Down Select Box” field type, when in the Profile Fields backend.

    I have structured the drop down list like this:

    5 feet 4 inches
    5 feet 5 inches
    5 feet 6 inches
    5 feet 7 inches
    5 feet 8 inches
    5 feet 9 inches
    5 feet 10 inches
    5 feet 11 inches
    6 feet 0 inches
    6 feet 1 inches
    6 feet 2 inches
    6 feet 3 inches

    My goal in BP Profile Search is to allow users to search between 2 different heights (as seen above). Such as searching for members who have heights between “5 feet 4 inches” and “6 feet 3 inches”

    Thank you,

    Jamie

    • This reply was modified 4 years, 10 months ago by jksastrology.
    Plugin Author Andrea Tarantini

    (@dontdream)

    Hello Jamie,

    To compare the height, the database only uses the first numeric value it sees in the selected string, in your case it will simply see the number 5 or the number 6.

    A better solution could be to use options like:

    167.64 cm (5’6″)
    170.18 cm (5’7″)
    172.72 cm (5’8″)

    Then, to enable the comparison, add this code to bp-custom.php:

    add_filter ('bps_xprofile_format', 'change_format', 10, 2);
    function change_format ($default, $field_id)
    {
    	if ($field_id == 143)  return 'decimal';
    	return $default;
    }

    so the form builder will offer the ‘range’ search mode as an option.

    If you prefer two drop-downs in your search form instead of two input boxes, add this code too:

    add_action ('bps_field_before_search_form', 'change_display');
    function change_display ($f)
    {
    	if ($f->code == 'field_143' && $f->display == 'range')
    		$f->display = 'range-select';
    }

    Replace 143 with the actual ID of your field.

    Thread Starter jksastrology

    (@jksastrology)

    Hello Andrea,

    Thank you for your response. And sorry for my late reply, I have been having trouble getting this height range code to work.

    Well, actually your height range code does work perfectly on its own, but throws up an error (Cannot redeclare change_display) when used with a similar code I have for “Age range” search. After reading about this error, it seems like a function cannot be called twice, even when used for different things. I am not sure how to get around this problem. Hopefully you or others may know the best solution to this issue. Thank you for your time.

    // Change bp member search form,  allow age to be selected from drop down //
    add_action ('bps_field_before_search_form', 'change_display');
    function change_display ($f)
    {
        if ($f->code == 'field_4' && $f->display == 'range')
        {
            $f->display = 'range-select';
            $range = range (18, 99);
            $f->options = array ('' => '') + array_combine ($range, $range);
        }
    
    // Change bp member search form,  allow Height to be selected from drop down //
    
    add_filter ('bps_xprofile_format', 'change_format', 10, 2);
    function change_format ($default, $field_id)
    {
        if ($field_id == 21)  return 'decimal';
        return $default;
    }
    
    add_action ('bps_field_before_search_form', 'change_display');
    function change_display ($f)
    {
        if ($f->code == 'field_21' && $f->display == 'range')
            $f->display = 'range-select';
    }
    
    }
    
    
    Plugin Author Andrea Tarantini

    (@dontdream)

    Jamie,

    You can merge the two ‘change_display’ functions:

    // Change bp member search form,  allow age to be selected from drop down //
    add_action ('bps_field_before_search_form', 'change_display');
    function change_display ($f)
    {
        if ($f->code == 'field_4' && $f->display == 'range')
        {
            $f->display = 'range-select';
            $range = range (18, 99);
            $f->options = array ('' => '') + array_combine ($range, $range);
        }
    
        if ($f->code == 'field_21' && $f->display == 'range')
        {
            $f->display = 'range-select';
        }
    }
    
    // Change bp member search form,  allow Height to be selected from drop down //
    add_filter ('bps_xprofile_format', 'change_format', 10, 2);
    function change_format ($default, $field_id)
    {
        if ($field_id == 21)  return 'decimal';
        return $default;
    }
    Thread Starter jksastrology

    (@jksastrology)

    Thank you Andrea. That worked : )

    Kind regards,

    Jamie

    Plugin Author Andrea Tarantini

    (@dontdream)

    Great, you’re welcome!

    Hi there, just curious about the suggested solution for adding a range search. I’m using another approach, Number type field and Range search mode, which is working fine.

    What is the purpose of using this height range code, rather than the Number type field and Range search mode solution I use?

    Regards
    Carsten

    Actually, as it turns out, I’m using the same code ??

    Plugin Author Andrea Tarantini

    (@dontdream)

    Yes, that code works with different field types.

    Choosing the field type affects the way the field is viewed and edited, while adding the suggested code affects the way the field is searched.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to search a height range in feet’ is closed to new replies.