AnonymousInactive
What’s proper way to get query parameter in sf_input_object_pre?
This:
sf_input_object_pre
will allow you to modify the html elements, yes. My bad. I am not sure how you would use it in this context though.
AnonymousInactive
I understand.
BTW, Can’t I use these hook to filter dropdown?
sf_edit_query_args or sf_input_object_pre
https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/
AnonymousInactive
Awesome! That was a huge help, thanks. In case anyone else is reading this to solve the same problem, here’s how I solved it:
We happen to have a page-search.php template in our child theme. So, I just added this to the very top:
$searchFilterID = 44867;
add_filter('sf_input_object_pre', 'insert_search_term_into_advanced_search', 10, 2);
function insert_search_term_into_advanced_search ($input_object, $searchFilterID)
{
$q = isset($_GET['q']) ? htmlspecialchars($_GET['q']) : '';
if($input_object['name']=='_sf_search') $input_object['attributes']['value'] = $q;
return $input_object;
}
AnonymousInactive
Hi,
I would like to have the checkboxes’ checked attribute automatically set to ‘checked’, when opening the form. I use the following filter function :
function catcheck($input_object, $sfid)
{
if($input_object[‘attributes’][‘type’]==’checkbox’)
{
$input_object[‘attributes’][‘checked’]=’checked’;
}
return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘catcheck’, 10, 2);
It does not work. In my various tests, I manage to act on the
element that contains the input elements, but not on the input elements themselves.
Thanks for your help.
Olivier
AnonymousInactive
Thanks Ross!
With this filter it’s possible to access to ‘heading’ label? I’ll need something like this:
function filter_function_name($input_object, $sfid)
{
$input_object['heading'] = apply_filters( 'the_title', $input_object['heading'] );
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
AnonymousInactive
Hello Trevor,
This looks like a script that would work, I guess.
How do I use this?
function filter_function_name($input_object, $sfid)
{
if($input_object['name']=='_my_field_name')
{
//udpate this field before rendering
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
AnonymousInactive
Hey Davide!
Sorry for the delay – hectic couple of weeks.
Try this:
// Relabel Co-Authors in Search & Filter
function filter_function_name($input_object, $sfid) {
if ($input_object['name'] == '_sft_author') {
global $coauthors_plus;
// Update option labels before rendering
foreach($input_object['options'] as $key => $option) {
// Rename "all items" label - this is always the option with no value
if($option->value=="") {
$option->label = "All Authors";
}
else {
$user = $coauthors_plus->get_coauthor_by( 'user_nicename', $option->value );
$input_object['options'][$key]->label = $user->last_name . ', ' . $user->first_name . ' ' . ' (' . $option->count . ')';
}
}
$sortArray = array();
foreach($input_object['options'] as $option) {
foreach($option as $key => $value) {
if(!isset($sortArray[$key])) {
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
$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);
AnonymousInactive
Thanks, Ross! 🙂
Sorry, Davide! I haven’t had a chance to look at this yet. Work got kinda busy. Bleurrrrgh!
As far as putting the last name first bit goes, that’s easy. All you have to do is change display_name to a combination of last_name, first_name. The code would look something like this:
// Relabel Co-Authors in Search & Filter
function filter_function_name($input_object, $sfid) {
if ($input_object['name'] == '_sft_author') {
global $coauthors_plus;
// Update option labels before rendering
foreach($input_object['options'] as $key => $option) {
// Rename "all items" label - this is always the option with no value
if($option->value=="") {
$option->label = "All Authors";
}
else {
$user = $coauthors_plus->get_coauthor_by( 'user_nicename', $option->value );
$input_object['options'][$key]->label = $user->last_name . ', ' . $user->first_name . ' ' . ' (' . $option->count . ')';
}
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
As for sorting, I think Ross is right and that the best way to go about it is using one of the standard PHP sorts.
I’ll have a look at doing that in the next couple of days. Would be a useful one to figure out! 🙂
AnonymousInactive
Done!
Added back the “All Authors” title and my post count. Final code:
// Trial by fire
function filter_function_name($input_object, $sfid) {
if ($input_object['name'] == '_sft_author') {
global $coauthors_plus;
//udpate this field before rendering
foreach($input_object['options'] as $key => $option) {
if($option->value=="")
{//the option with no value is always the "all items" or unselected state
$option->label = "All Authors";
}
else {
$user = $coauthors_plus->get_coauthor_by( 'user_nicename', $option->value );
$input_object['options'][$key]->label = $user->display_name . ' (' . $option->count . ')';
}
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);