AnonymousInactive
It does not respect the collation, which is set to utf8mb4_danish_ci
.
I had to use a filter to fix it:
function sfp_sort_scandinavian( $input_object, $sfid ) {
if ( '_sfm_employees' === $input_object['name'] ) {
usort( $input_object['options'], function( $a, $b ) {
return strcmp( $a->value, $b->value );
});
}
return $input_object;
}
add_filter( 'sf_input_object_pre', 'sfp_sort_scandinavian', 10, 2 );
Hi Boris
So I had a little play with this and got it half working – it will be for you to tidy up.
Basically, there is a PHP function for sorting arrays by properties – usort
(in this case we want to sort the array by count
) :
function cmp_reorder_desc($a, $b) {
return $a->count < $b->count;
}
function reorder_options_count( $input_object, $sfid ) {
// change _sft_level for your field name
if($input_object['name']!=='_sft_level') {
return $input_object;
}
if ( ! isset( $input_object['options'] ) ) {
return $input_object;
}
if ( ! is_array( $input_object['options'] ) ) {
return $input_object;
}
// $input_object['options'] // this is an array of options
// An option has 3 properties (and a few more):
//$option->label;
//$option->value;
//$option->count;
//this doesn't take into consideration the first option "All items" - you might want to remove it (it is the first option: )
// $input_object['options'][0] and re-add to the array, after the sorting
// we can use the PHP function to sort an array by "count":
echo usort( $input_object['options'], "cmp_reorder_desc");
return $input_object;
}
add_filter('sf_input_object_pre', 'reorder_options_count', 10, 2);
I added some notes in for you, remember to change the field name at the beginning to your own.
Thanks
AnonymousInactive
Hi Trevor,
thanks for the link, but I’m not sure what I should do now.
I understand I have to enter the field name it concerns:
function filter_function_name($input_object, $sfid)
{
if($input_object['name']=='_company_name')
{
//udpate this field before rendering
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
But beyond that, I don’t know how to trim it down to 10 and how to sort them by number. I went through the code on the github page that is referred to, but that doesn’t bring me further. Can you help me get this right?
It MIGHT be possible, but only with custom code (I don’t know what this would be though), using this filter:
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
In code similar to this (in the child theme functions.php file):
function filter_input_destination($input_object, $sfid)
{
if(($input_object['name']!='_sft_destination')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
// sort the array here, using PHP array_multisort() function.
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_destination', 10, 2);
And I would guess make sure it is the correct form ID.
AnonymousInactive
Thank you again Trevor, although in my case, the value actually does exist in the post data. Perhaps it would help if I gave a more specific example of what I am trying to do ie.
Option group: Shop, event or both
Select: Country
Unfortunately countries are stored as a numerical ID in my postmeta which is why I want to use add_filter to change the label for each of them. For now, the code below attempts to change the label for just one country ie. 13 = Austria.
Page loads correctly initially and shows all post types for all countries
If I select one post type plus a country, search results work fine.
If I select both post types, no results are shown.
If I try to Reset the form, no results are shown.
I just cannot understand what the add_filter is doing (or attempting to do) in order to cause such results.
function filter_input_object2($input_object, $sfid)
{
if(($input_object['name']!='_sfm_country_id')||($input_object['type']!='select'))
{
return $input_object;
}
if(!isset($input_object['options']))
{
return $input_object;
}
foreach($input_object['options'] as $option)
{
if($option->value=="13")
{
$option->label = "Austria";
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object2', 10, 2);
Note: yes I am aware there are ways of entering the postmeta labels manually and it seems that may be my only option but was reluctant to resort to this as it wouldn’t automatically handle new countries as/when they are added.
AnonymousInactive
Trevor,
Thanks for all your help. I was able to find some code and have adapted it to work the way we need. It’s below incase you need it for anyone else
function filter_function_name($input_object, $sfid) {
if ($input_object['name'] == '_sfm_attorneys') {
global $coauthors_plus;
// Update option labels before rendering
foreach($input_object['options'] as $key => $option) {
$my_query = new WP_Query( array( 'post_type' => 'attorney', 'p' => $option->value));
while ($my_query->have_posts()) : $my_query->the_post();
$att_first = types_render_field("att_first", array());
$att_last = types_render_field("att_last", array());
$input_object['options'][$key]->label = $att_last . ', ' . $att_first;
endwhile;
}
// Sort options...
$sortArray = array();
foreach($input_object['options'] as $option) {
foreach($option as $key => $value) {
if(!isset($sortArray[$key])) {
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
// ...by label
$orderby = "label";
array_multisort($sortArray[$orderby],SORT_ASC,$input_object['options']);
}
// Return
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
If you mean how they display in the dropdown select in the form, you would need to do this using our https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object filter.
I do not know if there are any snippets showing how other users have done anything similar, but you might find the posts that this search returns useful:
https://support.searchandfilter.com/forums/search/sf_input_object_pre+sort/
Are these meta keys being searched with a Range field, because that would cause the problem. At this time you can sort of work around it (by using the Edit Query Argument filter to remove the unwanted range parameters). There will be snippets other users have posted on how they did this if you search for:
sf_input_object_pre range
AnonymousInactive
All of a sudden the search forms have stopped sorting. The page is here: https://intrepidexposures.com/photo-tours/
Tweaks to code as per previous threads here:
// Search Filter
function filter_input_object($input_object, $sfid) {
if(($input_object['name'] == '_sfm_workshop_location') || ($input_object['name'] == '_sfm_tour_style') || ($input_object['name'] == '_sfm_tour_leader')) {
$new_options = array(); //the options added to this will replace all existing optoins in the field
foreach($input_object['options'] as $option) {
if ($option->value !== "")
{
//check to see if the option has a parent
$parent_id = wp_get_post_parent_id($option->value);
if(!$parent_id)
{
//then this option does not have a parent, so it must be a parent itself, add it to the new array
$option->label = get_the_title($option->value);
array_push($new_options, $option);
}
}
}
//now we have an array with only parent options in, so simply replace the one in the input object
$input_object['options'] = $new_options;
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Any ideas?
Are you saying that, on the S&F Pro form settings page, on the Posts tab, you tried it with the Primary sort as date and secondary sort as name? I would have thought that should have worked. as you say, there should be no need for the secondary sort, as the date would be sufficient.
My initial though would be that you could code around this using one of our filters. See here in the documentation.
If you use the filter sf_input_object_pre
you get access to the options as an array, so you can order this array however you want, so you can use this to sort the array with php. But you would need to code this.