Accessing Field slug on search results

Forums › Forums › Search & Filter Pro › Accessing Field slug on search results

Tagged: ,

Viewing 5 posts – 1 through 5 (of 5 total)
  • Anonymous
    #169166

    Just a quick question that I haven’t been able to find the answer to – I’m customising my results page to only display certain text etc if a search field has a value (see below) and I’d like to be able to access the slug for a certain field (exactly how it is in the search url) so I can load an image based upon this value.

    Full code is below, ideally I need something along the lines of:

    $brand = $sf_current_query->get_fields_html( array("_sft_pwb-brand"), $args );
    	if(!empty($brand)){echo '<img src="'. $brand .'.png">';};
    	echo '</p>';

    But with the brand-slug rather than “Brand Name”?

    <?php
    	global $searchandfilter;
    	$sf_current_query = $searchandfilter->get(1446)->current_query();
    	
    	$args = array(
    		"str" 					=> '%2$s', 
    		"delim" 				=> array(", ", " - "), 
    		"field_delim"				=> ' ', 
    		"show_all_if_empty"			=> false 
    	);
    		
    	echo '<p>All ';
    	echo $sf_current_query->get_fields_html( array("_sft_pa_size","_sft_product_cat"), $args );
    	$colour = $sf_current_query->get_fields_html( array("_sft_pa_colour"), $args );
    	if(!empty($colour)){echo ' in '. $colour;};
    	$brand = $sf_current_query->get_fields_html( array("_sft_pwb-brand"), $args );
    	if(!empty($brand)){echo ' by '. $brand;};
    	echo '</p>';
    		
    ?>

    Thanks in advance 🙂

    Trevor
    #169171

    Have you output the full results array to see if what you want is in there, and where it is:

    $sf_current_query = $searchandfilter->get(1446)->current_query();
    echo '<pre>';
    print_r($sf_current_query->get_array());
    echo '</pre>';
    Anonymous
    #169183

    Thank you Trevor – this outputs:

    Array
    (
        [_sft_product_cat] => Array
            (
                [name] => Product categories
                [singular_name] => Category
                [all_items_label] => All categories
                [type] => taxonomy
                [active_terms] => Array
                    (
                        [0] => Array
                            (
                                [id] => 128
                                [name] => Men's Shoes
                                [value] => mens-shoes
                                [count] => 0
                            )
    
                    )
    
            )
    
        [_sft_pwb-brand] => Array
            (
                [name] => Brands
                [singular_name] => Brand
                [all_items_label] => All Brands
                [type] => taxonomy
                [active_terms] => Array
                    (
                        [0] => Array
                            (
                                [id] => 220
                                [name] => Ted Baker
                                [value] => ted-baker
                                [count] => 0
                            )
    
                    )
    
            )
    
    )

    And the bit I need is the “value” of “_sft_pwb-brand” – i.e. “ted-baker”

    Thanks in advance 🙂

    Anonymous
    #169189

    I think I’ve sorted it thanks Trevor – sorry to mess you around!

    If anyone else needs it, the code is as follows:

     <?php
    
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(1446)->current_query();
    
    $array_data = $sf_current_query->get_array();
    $typeOfSearch = $array_data["_sft_pwb-brand"]["active_terms"][0]["value"];
    
    echo $typeOfSearch ;
    
    ?>

    Where _sft_pwb-brand is the field you need, and 1446 is the ID of the search form.

    Trevor
    #169193

    Great, and thanks for sharing. I will close this thread for now.

Viewing 5 posts – 1 through 5 (of 5 total)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.