Forums Forums Search Search Results for 'function sf_results_url'

Viewing 10 results - 1 through 10 (of 19 total)
  • Author
    Search Results
  • #288374

    Anonymous
    Inactive

    Provided by Stefan
    In case someone else runs into this problem, this works to add the form to multiple pages:

    function my_set_sf_resultsURL( $url, $sfid) {
    $url = get_permalink();
    return $url;

    }
    add_filter( ‘sf_results_url’, ‘my_set_sf_resultsURL’, 10, 2 );

    #276773

    Trevor
    Participant

    An update (on 2023-04-17) from a user (Michael Briel):

    This doesn’t work any more, since

    weglot_get_current_and_original_language()

    doesn’t exist any more in Weglot. Also the solution is for one case only (…/references/…).

    I created a new version of the fix that works even more flexible, since I use it on a page with several different search pages – it doesn’t use a fixed url, but takes the one incoming and uses it, no matter which – meaning: When the search page is http://www.abcd.com/testsearchpage/ it takes the “/testsearchpage/-part and uses it:

    function modify_sf_results_url( $url, $sfid ) {
    $current_language = weglot_get_current_language();
    $relative_url = str_replace( home_url(), "", $url );
    
    if ($current_language === "en") {
    // nothing, since en is main language - replace "en" with your main language if needed
    } else {
    $url = home_url("/".$current_language.$relative_url."");
    }
    return $url;
    }
    add_filter( 'sf_results_url', 'modify_sf_results_url', 10, 2 );
    #276745

    Anonymous
    Inactive

    In case someone else runs into this problem, this works to add the form to multiple pages:

    function my_set_sf_resultsURL( $url, $sfid) {
    $url = get_permalink();
    return $url;

    }
    add_filter( ‘sf_results_url’, ‘my_set_sf_resultsURL’, 10, 2 );

    #264091

    Anonymous
    Inactive

    Hi

    Thanks for the answer. When I add the anchor after the url it still comes before the filter part of the url.

    I have this code in my functions.php

    function filter_function_name( $url, $sfid ) {
    	if ( $sfid == 1019 ) {
    		return $url . '#producten';
    	}
    }
    add_filter( 'sf_results_url', 'filter_function_name', 10, 2 );

    and this returns http://ranobo.fluxwebdesign7.be/assortiment/#producten?_sf_s=amandelen when I search but I need it to return http://ranobo.fluxwebdesign7.be/assortiment/?_sf_s=amandelen#producten.

    Is this possible?

    Thanks

    #262667

    Anonymous
    Inactive

    I actually think I managed to write this to be more dynamic – might be something to keep open or add to documentation.

    
    	function modify_sf_results_url( $url,  $sfid ) {
    
    		//We stock the current language in a variable
    		$current_and_original = weglot_get_current_and_original_language();
    		$current_language = $current_and_original['current'];
    		$original_language = $current_and_original['original'];
    			
    		if($current_language == $original_language){
    			// do nothing
    		} else {
    
    			$url = home_url("/".$current_language."/videos/");
    			return $url;
    		};
    		
    	}
    	add_filter( 'sf_results_url', 'modify_sf_results_url', 10, 2 );
    

    with the /video/ addition needing to be adjusted to suit your sites search and filter root S&F url.

    #260181

    Trevor
    Participant

    I think we may need to use this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#modify-urls

    It would go in the child theme functions.php file.

    This forum search should give some example snippets:

    https://support.searchandfilter.com/forums/search/function+sf_results_url/

    #257967

    Trevor
    Participant

    You can, but only by writing you own custom code (although there will be snippets you can probably draw on by searching the forum:

    https://support.searchandfilter.com/forums/search/function+sf_results_url/

    You will need to use this filter in your child theme functions.php file:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    #253659

    In reply to: Form on a single-post


    Anonymous
    Inactive

    I’ve solved it with some workarounds. I will post my solution for other people:

    Form settings:
    – configured the form display method as ‘custom’;
    – added a results url of a random page (it needs to be filled);
    – enabled Ajax loading and configured a div as Ajax container;
    – enabled ‘make searches bookmarkable’;
    – enabled Ajax pagination.

    I added a filter in functions.php, so the results url for this specific form will always be overwritten with the current permalink:

    
    function filter_function_name( $url,  $sfid) {
    	if (get_post_field('post_name', $sfid) == 'agenda-shadow-single-post') {
    		$url = get_permalink();
    	}
    	return $url;
     }
     add_filter( 'sf_results_url', 'filter_function_name', 10, 2 );
    

    I implemented the form in template files as following:

    
    <?php
    	// retrieving the the form by using the slug
    	$sf_form_obj = get_page_by_path('agenda-shadow-single-post', OBJECT, 'search-filter-widget');
    
    	if ($sf_form_obj) {
    		$args['search_filter_id'] = $sf_form_obj->ID;
    		$query = new WP_Query($args);
    
    		if ($query->have_posts()) {
    			while ($query->have_posts()) {
    				$query->the_post();
    
    				get_template_part('template-parts/blocks/block-activiteit');
    			}
    		} else {
    			?>
    				<div class='search-filter-results-list' data-search-filter-action='infinite-scroll-end'></div>
    			<?php
    		}
    	}
    ?>
    

    I tried to alter the query args with the sf_edit_query_args filter, but it didn’t work for me. So I wrote some JavaScript to modify the value of the filter of the post-type that’s currently being viewed. After that I’ve hidden the filter, so it won’t be changed by the user. I did this with a function that’s being triggered on these ajax events:

    jQuery(document).on("sf:ajaxstart sf:init sf:ajaxfinish", ".searchandfilter", function () {
    	fillFilterOnLocation();
    });
    
    #222767

    Anonymous
    Inactive

    The Problem: I need to load an author dropdown on the filter with just the Editors

    I have a Filter for my site, with several fields, one of them is Author, but the website has more than 15.000 users registered and get a timeout error every time the field is enabled.

    I’ve tried this to re write the author field with a more manageable amount of users (only the Editors) but with no luck the idea was to remove all content from _sf_author and replace it with the editors

    `
    function filter_authors($input_object, $sfid)
    {
    if (($input_object[‘name’] != ‘_sf_author’) || ($input_object[‘type’] != ‘select’)) {
    return $input_object;
    }

    unset($input_object[‘options’]);
    $input_object[‘options’] = array();

    // Generate first array option (default)
    $first_option = new StdClass();
    $first_option->value = ”;
    $first_option->label = ‘All Authors’;

    //attributes
    $first_option->attributes = array(
    ‘title’ => ‘All Authors’,
    );

    array_push($input_object[‘options’], $first_option);

    //change classes & attributes of the field
    $input_object[‘attributes’][‘class’] = ‘user_filter’;
    $input_object[‘attributes’][‘title’] = ‘Authors’;

    //add/override prefix & postfix to the field
    $input_object[‘prefix’] = “Filter by Author”;

    //Check if options variable exists
    if (!isset($input_object[‘options’])) {
    return $input_object;
    }

    //Create new users array
    $args = array(
    ‘role’ => ‘editor’,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘display_name’,
    );

    // Create the WP_User_Query object
    $wp_user_query = new WP_User_Query($args);
    $authors = $wp_user_query->get_results();
    foreach ($authors as $author) {
    // get all the user’s data
    $author_info = get_userdata($author->ID);
    //create new options with user values
    $new_option = new StdClass();
    $new_option->value = $author_info->user_login;
    $new_option->label = $author_info->first_name . ‘ ‘ . $author_info->last_name;

    //attributes
    $new_option->attributes = array(
    ‘title’ => $author_info->user_login,
    );
    array_push($input_object[‘options’], $new_option);
    }
    return $input_object;
    }
    // add_filter(‘sf_input_object_pre’, ‘filter_authors’, 10, 2);
    `
    Using this approach i change a meta field and filled it with the Editors data, but then the post request was send as _sfm_editors and not _sf_author and i couldn’t find a way to replace this request for the one i needed.

    Also tried building my own query accessing the search data and using the hook sf_results_url to change the url, but i cannot change the search query with it.

    I’m running out of ideas, appreciate your time to check my issue and any new ideas on how to solve it.


    Ross
    Keymaster

    Hi all

    So the original example from Mariusz was the right way to do it:

    function change_sf_results_url( $url,  $sfid) {
    
        //if search form ID = 225, the do something with this query
    	if($sfid==922)
    	{
            $url = add_query_arg('tab', '#promo', $url);
            return $url;
    	}
    }
    add_filter( 'sf_results_url', 'change_sf_results_url', 10, 2 );

    But as mentioned, this is the base URL, the URL to which our search parameters get appended to, so whatever arguments you have here, will have other args appended after in the URL once a search has been performed.

    Can I ask, why does the argument have to be last in the list?

    Thanks

Viewing 10 results - 1 through 10 (of 19 total)