Support Forums

Looking for support? You can access the support system via your account.

Forums Forums Search Search Results for 'sf_current_query get_array'

Viewing 10 results - 1 through 10 (of 115 total)
  • Author
    Search Results
  • #269562

    Cliff Demandt
    Participant

    Hello,

    At the moment i am building a website where people can filter posts. Once filtered it is the intention that it should be possible for people to leave their emailadress. Once they do that i save the “$sf_current_query->get_search_term()” and “$sf_current_query->get_array()” to the database to make it possible to repeat the query.

    Would it be possible to use the saved data to query the results later on? if so with which function? I already tried “set_field_values_array()” but i cannot get it to work.

    The results do not need to be filtered, i just want to check if there are any results afterwords with WP_Query.

    Hope that you can help me with the above, thanks in advance!

    #268116

    Michael Creighton
    Participant

    I tried following this: https://searchandfilter.com/documentation/accessing-search-data/

    on this page: http://staging2.iconcasterwheels.com/shop/?_sfm_caster-wheel-size=10%22&_sfm_caster-wheel-material=Air%2FFlat%20Free&_sfm_caster-fastening-type=Top%20Plate&_sfm_caster-load-capacity=200+300

    Here is the code:

    global $searchandfilter;
    	  $sf_current_query = $searchandfilter->get(56)->current_query();
          //var_dump($sf_current_query->get_array());
    
          $searchArgs = array(
            "str" 					=> '%2$s', 
            "delim" 				=> array(", ", " - "), 
            "field_delim"			=> ', ', 
            "show_all_if_empty"		=> false 
          );
    	  
          
          echo $sf_current_query->get_fields_html(
            array(
              "_sfm_caster-wheel-size",
              "_sfm_caster-wheel-material",
              "_sfm_caster-fastening-type",
              "_sfm_caster-load-capacity"
            ), 
            $searchArgs
          );
    
    

    When I do a var_dump, it shows the info that is expected (hidden for now) — so the id and everything seems to be correct.
    However, when I try to display any of the field values, nothing shows up.

    No errors in console message or anything either.

    I am trying to get it to say “Search Results 10″, Air Flat/Free, Top Plate”

    #264202

    Trevor
    Moderator

    If you are using Ajax, you need these filter labels inside the Ajax Container. If you want them to be shown elsewhere on the page, you still have them, but hidden with CSS, inside that Ajax Container, but use jQuery to copy and paste them to where they will show after our sf:ajaxfinish event has triggered.

    You will also need to be using a PHP template for the page. It will be much harder (or not possible at all) if you are using a page builder or post grid plugin/widget.

    It will require some coding for which I can point you in the direction of snippets, but I cannot help you write the code, sorry, as this is not within the scope of our support.

    To fetch the search terms, the https://searchandfilter.com/documentation/accessing-search-data/ guide is basic but you can extend the idea to grab lots of other data. If you have other filters, then it becomes a little more complex, but I can give you links. This thread might help you:

    https://support.searchandfilter.com/forums/topic/accessing-field-slug-on-search-results/

    … and this search will give similar threads I think:

    https://support.searchandfilter.com/forums/search/sf_current_query+get_array

    This particular post may help:

    https://support.searchandfilter.com/forums/topic/acf-post-meta-fields-in-current_query-result/#post-249704

    I would add that we plan to make this far easier in V3 of our plugin, due in a few months.

    #262294

    Danny Albeck
    Participant
    This reply has been marked as private.
    #259956

    Odi
    Participant

    Hi Trevor,
    Thank you for the response. The code you sent works well, however, it needs to be in the archive page itself to work, which is something I’m trying to avoid because that would mean the page has to reload to display the active filters.

    I tried to run it through AJAX once the form submits. But I’m not getting any results. (below is the function I added to functions.php) (the ajax request works fine, so the issue is with $searchandfilter).

    Is there any way to use the “$searchandfilter” class and the “current_query()” method from OUTSIDE the archive page?

    Please let me know. thank you.

    add_action( 'wp_ajax_show_active_filters',  'show_active_filters' );
    add_action( 'wp_ajax_nopriv_show_active_filters', 'show_active_filters' );
    function show_active_filters(){
        global $searchandfilter;
        $sf_current_query = $searchandfilter->get(8175)->current_query()->get_array();
        ob_start();
        
        echo '<span class="active-pill">'.$sf_current_query['_sft_contest-years']['active_terms'][0]['name'].'</span>';
    
        $ajax_filters_html .= ob_get_contents();
        ob_end_clean();
        echo json_encode(array('html'=>$ajax_filters_html));
        wp_die();
    }
    #259932

    Odi
    Participant

    I found this class that allows to output the current filters

    <?php
    	//Get an array of objects containing data for the current search/filter
    	//replace <code>1526</code> with the ID of your search form
    	global $searchandfilter;
    	$sf_current_query = $searchandfilter->get(1526)->current_query();
    	var_dump($sf_current_query->get_array());
    ?>

    But what I’m really looking for is to dynamically display the filter terms AND search term on Submit (AJAX enabled). Also the ability to Clear those Filters / Search items one by one (X them out).

    Please let me know if this is doable?

    #256352

    Trevor
    Moderator

    You would need to code some PHP. As you are using the Shortcode display results method, start with this guide (if you have not already):

    https://searchandfilter.com/documentation/search-results/using-a-shortcode/#customising-the-results

    To fetch the search terms, the https://searchandfilter.com/documentation/accessing-search-data/ guide is basic but you can extend the idea to grab lots of other data. If you have other filters, then it becomes a little more complex, but I can give you links. This thread might help you:

    https://support.searchandfilter.com/forums/topic/accessing-field-slug-on-search-results/

    … and this search will give similar threads I think:

    https://support.searchandfilter.com/forums/search/sf_current_query+get_array

    Note, if you are using Ajax refreshing of the results, any PHP needs to be inside the Ajax Container, or it will not update.

    #253147

    Trevor
    Moderator

    The code you would need is:

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(9490)->current_query()->get_array();
    echo $sf_current_query['_sfm_UnitLocation']['active_terms'][0]['name'];

    Look carefully, as only the ‘global’ line is the same.

    Note, if you are using Ajax on the page, you may need to make sure that the page title is inside the Ajax Container, otherwise it will not refresh and change.

    #252362

    Yaniv Schneider
    Participant

    Hi Trevor,

    I have some questions regarding the usage and hope you can help me.

    1. A drop down filter with custom ACF field. I have a front-end form created with ACF where one of the fields is Price. I have created a main filter where i use the range field with two values minimum price and maximum price and it works great. Now i am looking to create another small drop down filter with two options: Sort posts by lowest price and Sort posts by highest price. Sorry if it is obvious, but i could not find how can i achieve it using one single ACF field.

    2. The reset button does not work when filter is not submitted with AJAX. Is it how it should behave or am i missing something?

    3. Last thing, and perhaps it is a feature request. Would it be possible to make echoed filter labels clickable, so that each label resets/clears the corresponding filter input?

    I echo the filters labels using this method:

    
    global $searchandfilter;
    
    $sf_current_query = $searchandfilter->get(15197)->current_query()->get_array();
    
    if( isset( $sf_current_query['_sfm_watch_year']['active_terms'] ) ) :
    
            $get_years = $sf_current_query['_sfm_watch_year']['active_terms'];
    
            $show_year = '';
    
            if ( $get_years ) {
    
                foreach( $get_years as $value ) {
                    $show_year .= '<span class="fl">' . $value['name'] . '</span>';
                }
    
                $label_year = 'Years: ' . rtrim( $show_year, ', ' );
    
            }
    
        endif;

    This code show the labels of years checked checkboxes. What i mean is that years label will have a link and the click event will reset the years filter checkbox.

    Please see an example

    Thank you!


    Trevor
    Moderator
    This reply has been marked as private.
Viewing 10 results - 1 through 10 (of 115 total)