-
AuthorSearch Results
-
February 4, 2023 at 4:13 pm #276739
In reply to: Display title instead of ID in Post Meta
AnonymousInactive@Jermey Michel and @Trevor
Maybe this will help:
function filter_input_labels($input_object, $sfid) {
if($input_object[‘name’] == ‘_my_field_name’) {/* var_dump($input_object); */
// Fetch the options for this field
$options = $input_object[‘options’];// Loop through the options and update the label for each one
foreach($options as &$option) {
$post_id = $option->value;
$post = get_post($post_id);
$title = $post->post_title;$option->label = $title;
}// Update the options for this field
$input_object[‘options’] = $options;
}return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘filter_input_labels’, 10, 2);It worked for me 🙂
February 2, 2021 at 12:16 pm #274982In reply to: Filter order is wrong for Scandinavian characters
AnonymousInactiveIt 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 );
February 1, 2021 at 1:33 pm #274760In reply to: Show ONLY current category’s subcategories
RossKeymasterHi Danny
Writing something like that is a little out of scope of support (should require custom development) however I’ve gone ahead and bashed something toegether for you anyway (I won’t be able to customise this for you):
function sf_filter_options_sub_terms( $input_object, $sfid ) { $filter_name = '_sft_category'; $taxonomy_name = 'category'; $parent_term_id = 2; // you need to figure out the ID of the parent if ( $input_object['name'] !== $filter_name ) { return $input_object; } if ( ! isset( $input_object['options'] ) ) { return $input_object; } if ( ! is_array( $input_object['options'] ) ) { return $input_object; } // ask WP for the child terms $child_terms = get_terms( $taxonomy_name, array( 'parent' => $parent_term_id, 'orderby' => 'slug', 'hide_empty' => false ) ); // init the first option $default_option = new StdClass(); $default_option->value = ''; $default_option->label = 'Choose a Category'; // setup the new options array $child_options = array( $default_option ); // grab a reference to the search form global $searchandfilter; $search_form = $searchandfilter->get( $sfid ); // loop through child terms foreach ( $child_terms as $child_term ) { // add back in the dynamic count $term_count = $search_form->get_count_var( $filter_name, $child_term->slug ); //create a new option for the term $new_option = new StdClass(); $new_option->value = $child_term->slug; $new_option->label = $child_term->name; $new_option->count = $term_count; array_push( $child_options, $new_option ); } // replace the old options with the new ones $input_object['options'] = $child_options; return $input_object; } add_filter('sf_input_object_pre', 'sf_filter_options_sub_terms', 10, 2);
What you need to do is change the 3 variables at the top to match your own:
$filter_name = '_sft_category'; $taxonomy_name = 'category'; $parent_term_id = 2
The first
$filter_name
is the name of your field in our plugin, which will be –_sft_product_cat
The second is the actual taxonomy internal nameproduct_cat
And the third is the WP ID of the parent Product category (you can usually figure this out from WP admin by editing the term, and checking the URL)Once you replace those, this code will replace the options, with the child options of
$parent_term_id
.Let me know how you get on.
Thanks
January 29, 2021 at 1:22 pm #274552In reply to: Prefix values in options
AnonymousInactiveOk! so how could I make a checked option?
add_filter( ‘sf_input_object_pre’, ‘filter_function_name’, 10, 2 );
function filter_function_name( $input_object, $sfid )
{
if ( $input_object[‘name’]==’_sfm_aff_price_type’ ) {
//udpate this field before rendering
}return $input_object;
}It returns this object:
input_object:Array
(
[name] => _sfm_aff_price_type
[defaults] => Array
(
[0] =>
)[options] => Array
(
[0] => stdClass Object
(
[label] => Mensual
[count] => -1
[attributes] => Array
(
[class] => sf-level-0
)[value] => month
)[1] => stdClass Object
(
[label] => Trimestral
[count] => -1
[attributes] => Array
(
[class] => sf-level-0
)[value] => quaterly
))
[attributes] => Array
(
[data-operator] => and
[class] =>
)[accessibility_label] =>
[type] => checkbox
[input_class] => sf-input-checkbox
)Which value do I have to change to make month checked in radio?
January 27, 2021 at 5:48 pm #274292
TrevorParticipantAssuming that this is Post Categories, the easiest thing is to hide the All Categories with Custom CSS.
.sf-field-category .sf-item-0 { display: none; }
But, if it is a custom taxonomy, the name would be different, so you would need to substitute the class
.sf-field-category
with whatever the taxonomy you are using has as a class in our form.Is ‘This months featured blogs’ a category already in the list? If it is, you may need to re-order the list of terms. This is not so easy, but would use this filter (there are many other snippets of code using this filter in our forum):
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
Snippet search:
https://support.searchandfilter.com/forums/search/function+sf_input_object_pre/
But, what is not possible would be to pre-select that option on loading the form for the first time. I think that feature will arrive with V3 of our plugin in a few months time.
January 20, 2021 at 3:41 pm #273326In reply to: Customise text in drop down menus
TrevorParticipantFor Taxonomies, the only way to do that would be to use this filter to change the label:
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object
There are many snippets of code using that filter in the forums:
https://support.searchandfilter.com/forums/search/function+sf_input_object_pre/
January 11, 2021 at 9:17 am #272280In reply to: Blank options appear in search filter
RossKeymasterHi Keith
We have a filter here for modifying all input fields:
https://gist.github.com/rmorse/7b59b45a14b1ca179868Essentially, the only part you need from that is you need to loop through the options:
function remove_empty_options($input_object, $sfid ) { //ensure we are only filtering the correct field name - in this case the field we want to filter has the name <code>_sfm_attorneys</code> if(($input_object['name']!='_sfm_attorneys')){ return $input_object; } if(!isset($input_object['options'])) { return $input_object; } $new_options = array(); foreach($input_object['options'] as $option) { if ( trim( $option->label ) !== '' ) { array_push( $new_options, $option ); } } $input_object['options'] = $new_options; return $input_object; } add_filter('sf_input_object_pre', 'remove_empty_options', 10, 2);
I haven’t actually tested this, but it looks to be correct.
You will need some PHP development skills to modify – that should go in your child themes
functions.php
.Best
December 28, 2020 at 12:25 pm #271160In reply to: Filter by year and separate sort control
TrevorParticipantOK, and for anybody who has the same conditions that the previous user has, here is a demo form:
https://sandfdev.cdnwebservices.net/shop/
There are 5 ‘posts’, each has one ‘date’, each in a different year (2016-2020 inclusive).
The ‘Year’ field is based on a custom field named
first_date_from
, and this is an ACF date field with a single date saved in the format YYYYMMDD.The code below is pasted in to the child theme functions.php file:
function update_date_field_options($input_object, $sfid){ // ensure we are only filtering the correct field name in the correct form - in this case the field we want to filter has the name '_sfm_first_date_from', based on the field name 'first_date_from' if( $input_object['name'] == '_sfm_first_date_from' && $sfid == 42 ) { // if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types) if( ! isset( $input_object['options'] ) ) { return $input_object; } //udpate this field before rendering //all the options are stored in $input_object['options'] as an array $new_options = array(); //create a new "default" option $new_option = new StdClass(); $new_option->value = ""; $new_option->label = "All Years"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "20160101+20161231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2016"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add $new_option = new StdClass(); $new_option->value = "20170101+20171231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2017"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add $new_option = new StdClass(); $new_option->value = "20180101+20181231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2018"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add $new_option = new StdClass(); $new_option->value = "20190101+20191231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2019"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add $new_option = new StdClass(); $new_option->value = "20200101+20201231"; //this is the value that goes in the URL, which affects the query $new_option->label = "2020"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add //now replace the options with our own custom options: $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'update_date_field_options', 10, 2);
I hope you can see how it works by actually sending a date range of 1 Jan to 31 Dec of each year to the form. So, if you want more years, add them as you need.
This is how the field is set:
December 23, 2020 at 4:54 pm #270963In reply to: Select specific taxomony terms to use in filter
RossKeymasterHi Jessica
I think I see what is happening!
So I just tried your code (albeit changed it for my setup) and the strangest thing was happening, I was getting some checkboxes with labels missing, and some working just fine.
I then realised (and didn’t know) that at least for checkboxes, the array of
options
must be in order.So unsetting specific values leaves “holes” in the array, that for some reason our checkbox code can’t work around for now.
Instead I did this (push the wanted options, into a new array):
function filter_input_object($input_object, $sfid) { if( !in_array( $input_object['name'], array('_sft_pa_color') ) ) { return $input_object; } if( $input_object['name'] == '_sft_pa_color' ) { $new_options = array(); foreach($input_object['options'] as $key => $option) { if ( in_array( $option->value, array( 'gray', 'green') ) ) { $new_options[] = $option; } } $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
Another way would be to keep your code exactly the way it was, but before returning
$input_object
do:$input_object['options'] = array_values( $input_object['options'] );
This will effectively reset the indexes too.
Let me know how you get on!
Thanks
December 23, 2020 at 8:21 am #270878In reply to: Using choice fields to filter preset date ranges
TrevorParticipantWith a date field, there are only two ways I can think to do this.
1. Have an additional custom field in the posts where one of those 3 labels is the choice.
2. Use code. How this would work is:# Set the form UI field to a number range field, like this (in this case I chose a radio field, but a dropdown would also work, I think):
https://www.screencast.com/t/rpY6G9KLnX
Ignore my field name! Use what you have as a field name.
Then add code like this to your child theme functions.php file (change the field name, form ID number and dates to work for you):
function update_field_options($input_object, $sfid){ // ensure we are only filtering the correct field name in the correct form - in this case the field we want to filter has the name '_sfm_first_date_to', based on the field name 'first_date_to' if( $input_object['name'] == '_sfm_first_date_to' && $sfid == 42 ) { // if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types) if( ! isset( $input_object['options'] ) ) { return $input_object; } //udpate this field before rendering //all the options are stored in $input_object['options'] as an array $new_options = array(); //create a new "default" option $new_option = new StdClass(); $new_option->value = ""; $new_option->label = "All Ages"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "19900101+29991231"; //this is the value that goes in the URL, which affects the query $new_option->label = "Latest"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add $new_option = new StdClass(); $new_option->value = "0+19561231"; //this is the value that goes in the URL, which affects the query $new_option->label = "Golden Age"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "19570101+19891231"; //this is the value that goes in the URL, which affects the query $new_option->label = "Silver Age"; //the label can be anything array_push($new_options, $new_option); //now replace the options with our own custom options: $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
You can see what it looks like on this demo I made (it is not functional as the data does not match!!):
https://sandfdev.cdnwebservices.net/shop/
I hope that makes sense!
It assumes that the date data is correctly stored in ACF/SQL date format(YYYYMMDD).
-
AuthorSearch Results