Skip Navigation

[Résolu] Preselection of radiobutton in Custom Search

This support ticket is created Il y a 5 années et 9 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par Minesh Il y a 5 années et 8 mois.

Assisté par: Minesh.

Auteur
Publications
#1210557

Hi,

I need to have preselected first option in the radiobutton set. So I am using jQuery code. But how to display results with filters that are preselected with the jQuery on the first load of the page (search results are updated every time an input changes with AJAX). Or is there any other way how to simply preselect option?

jQuery(document).ready(function($) {
jQuery('input[name=my-input]:first').attr('checked', true);
});
#1210595

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - what if you try to trigger the click event of javascript.

For example:

jQuery(document).ready(function($) {
jQuery('input[name=my-input]:first').attr('checked', true).trigger('click');
});
#1210725

Hi,

thanks for the help, I had to modify the code (I have removed .attr('checked', true) ), because it did not work:

jQuery(document).ready(function($) {
jQuery('input[name=my-input]:first').trigger('click');
});

So the search page now has preselected radiobuttons, which are working now. But the search results will display twice. Once like they are not selected and then after some miliseconds it will reload again with the preselected buttons. Isn´t there any other option which can ensure that the search page will load imediatelly with the selected options? See lien caché

#1211028

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

There is another way to do that - you need to use the view's wpv_query_filter hook.

For example:

add_filter( 'wpv_filter_query', 'add_facetwp_to_views_query', 10, 3 );
function add_facetwp_to_views_query( $query_args, $view_settings, $view_id ) {

    if( $view_id==9999) {
      // add your query argument here
    }
    return $query_args;
}

Where:
- Replace 9999 with your original view ID
-

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you do not know how to do it, please feel free to share access details and I'll add the required filter for you.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1213230

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Well - you need to add the following filter to pre-filter the view's results:

add_filter( 'wpv_filter_query', 'func_modify_view_query', 10, 3 );
function func_modify_view_query( $query_args, $view_settings, $view_id ) {
 
    if( $view_id==1398) {
      
      if(empty($query_args['meta_query'])){
        $query_args['meta_query'] = array(
            	array(
                    'key' => 'wpcf-vhodne-pro',
                    'value' => 'Začátečníky',
                    'type' => 'CHAR',
                    'compare' => '='
                ),

            'relation' => 'AND'
        );
        
      }
      
    
    }
    return $query_args;
}

I've added the above code here with the name "toolset-custom-code":
=> lien caché

#1213234

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

In addition to this, I just checked that you are using the checkboxes field for the "wpcf-vhodne-pro" and for that you want to pre-filter the view with the value "Začátečníky".

Checkboxes fields are stored with special format as serialized array and that is why we will not be able to add the pre-filter on it. I suggest you should convert your custom field "wpcf-vhodne-pro" to taxonomy and then we will be able to filter the results.

Once you change the custom field to taxonomy, please let me know, I will share the code to pre-filter the results before view load on page.