Forums Forums Search Search Results for 'sf_current_query active_terms'

Viewing 10 results - 31 through 40 (of 50 total)
  • Author
    Search Results
  • #108703

    Anonymous
    Inactive

    GOT IT!!!

    global $searchandfilter;
    
     $sf_current_query = $searchandfilter->get(504)->current_query()->get_array();
    
     if (empty($sf_current_query)) {
        echo ('All Articles');
        }
     else {
       echo $sf_current_query['_sft_category']['active_terms'][0]['name'];
     }

    The above code worked like a charm, I also use the “Archive” setting as you suggested! Thanks so much for your help on this one!! Finally working properly!! 🙂

    #108628

    Anonymous
    Inactive

    Hi Trevor,

    UPDATE! I’ve managed to half get it working!!!

    It now works for all categories except the “All Categories” one, this seems to behave differently to the others and the code I have used doesn’t bring it in:

    $sf_current_query = $searchandfilter->get(766)->current_query()->get_array();
     echo $sf_current_query['_sft_category']['active_terms'][0]['name'];

    I’ve left the printr statement in on the live site so you can get a picture of what is happening. I presume there may need to be an if statement added but I have yet to be able to pull in the “All categories” heading… I was however thinking of just hard coding a title in as it is only one title?? But i’m not sure how I would do it. I’m sure there is a “proper” fix to this though?

    I hope you have a good weekend

    #95975

    Anonymous
    Inactive

    I found some workaround, to display currently selected value:

    $tmp = $sf_current_query->get_array();
    echo $tmp["_sfm_character"]["active_terms"][0]["name"];
    

    But it may be a bug in get_field_string method from documentation, which is not working in my case:

    echo $sf_current_query->get_field_string("_sfm_character");

    #90518

    In reply to: Post Meta Date Range


    Ross
    Keymaster

    Hi Max

    I had a look, it seems to me there is something wrong with your code sample.

    The filter receives a $query_args object, which contains the wp_query args that S&F defines, and at the end of the function it needs to return this $query_args object.

    Inside the function you are free to modify this as you want, which it looks like you got close to do doing but not quite there..

    You add your meta query to a new variable called $args, which never gets used.

    I would do it like the following (untested – please also copy & paste into editor to see my comments, this is a bit clunky):

    <?php
    function filter_function_name( $query_args, $sfid ) {
    
    	if( $sfid == 940 ) {
    
    		// I would use some known values for testing (I just made some dates up here)
    		$tender_start = "20170101";
    		$tender_end = "20170101";
    		
    		/*global $searchandfilter;
    		$sf_current_query = $searchandfilter->get(940)->current_query();
    		$array_data = $sf_current_query->get_array();
    		$tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value'];
    		$tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];*/
    		
    		//if we want to play nice and not completely replace the existing meta query args 
    		//there might be, then we need to check if some meta queries already exist and treat it differently
    		$meta_query = array(
    			'relation' => 'AND',
    			array(
    				'key'	  => 'tender_start',
    				'compare' => '>=',
    				'value'	  => $tender_start
    			),
    			array(
    				'key'	  => 'tender_end',
    				'compare' => '<=',
    				'value'   => $tender_end
    			)
    		);
    		
    		if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query'])))
    		{//this means there are some meta queries already here, so push this one onto the end
    			array_push($query_args['meta_query'], $meta_query);
    		}
    		else
    		{//else there aren't any meta queries
    			//$query_args['meta_query'] should always be an array of meta queries according to wp docs
    			$query_args['meta_query'] = array($meta_query);
    		}
    		
    		// not sure if you need these, you should set the order via the S&F admin (in the <code>posts</code> tab)
    		$query_args['meta_key'] = 'tender_start';
    		$query_args['orderby'] = 'meta_value_num';
    		$query_args['order'] = 'ASC';
    		
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    I’ve written that in my editor so it should be correct syntax, but I haven’t actually tested it, its just showing you how to properly use the $query_args to make the changes you want.

    Hope that helps

    #89576

    In reply to: Post Meta Date Range


    Anonymous
    Inactive

    Thank for your answer. Do you have a code sample? I tried some but no result.

    function filter_function_name( $query_args, $sfid ) {
    
    	if( $sfid == 940 ) {
    
    		global $searchandfilter;
    		$sf_current_query = $searchandfilter->get(940)->current_query();
    
    		$array_data = $sf_current_query->get_array();
    		$tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value'];
    		$tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];
    
    		$args = array(
    			'meta_key'       => 'tender_start',
    			'orderby'        => 'meta_value_num',
    			'order'          => 'ASC',
    			'meta_query'     => array(
    				'relation' => 'AND',
    				array(
    					'key'	  => 'tender_start',
    					'compare' => '>=',
    					'value'	  => $tender_start
    				),
    				array(
    					'key'	  => 'tender_end',
    					'compare' => '<=',
    					'value'   => $tender_end
    				)
    			)
    		);
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Trevor
    Participant

    Marjan, we seem to have successfully coded up the solution for this, using a PHP snippet:

    <?php
      global $searchandfilter;
      $sf_query = $searchandfilter->get(3217)->current_query();
      if ($sf_query->is_filtered()) {
        $sf_current_query = $searchandfilter->get(3217)->current_query()->get_array();
        echo "<b>Prikaz rezultatov za...</b>";
        $cat_string = "";
        foreach($sf_current_query['_sft_category']['active_terms'] as $item) {
          $cat_string .= $item['name'] . ", ";
        }
        $starost_string = "";
        foreach($sf_current_query['_sfm_starost']['active_terms'] as $item) {
          $starost_string .= $item['name'] . ", ";
        }
    if (($sf_current_query['_sft_category']['active_terms'][0]['name'])<>"") echo "<div>PRODUKTI: " . substr($cat_string, 0, -2) . "</div>";
    if (($sf_current_query['_sft_post_tag']['active_terms'][0]['name'])<>"") echo "<div>STANJA: " . ($sf_current_query['_sft_post_tag']['active_terms'][0]['name']) . "</div>";
    if (($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name'])<>"") echo "<div>KAKOVOST SPREMEMBE: " . ($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name']) . "</div>";
    if (($sf_current_query['_sfm_starost']['active_terms'][0]['name'])<>"") echo "<div>STAROST: " . substr($starost_string, 0, -2) . "</div>";
    if (($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name'])<>"") echo "<div>VRSTA PRIMERA: " . ($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name']) . "</div>";
    }
    ?>

    So I will close this now. Great to speak with you on Skype.


    Trevor
    Participant

    OK, nearly there. This is the code we have so far:

    <?php
      global $searchandfilter;
      $sf_query = $searchandfilter->get(3217)->current_query();
      if ($sf_query->is_filtered()) {
    ?>
    <div>Found <?php echo $sf_query->found_posts; ?> Results</div>
    <?php
      $sf_current_query = $searchandfilter->get(3217)->current_query()->get_array();
    if (($sf_current_query['_sft_category']['active_terms'][0]['name'])<>"") echo "<div>KATEGORIJE: " . ($sf_current_query['_sft_category']['active_terms'][0]['name']) . "</div>";
    if (($sf_current_query['_sft_post_tag']['active_terms'][0]['name'])<>"") echo "<div>OZNAKE: " . ($sf_current_query['_sft_post_tag']['active_terms'][0]['name']) . "</div>";
    if (($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name'])<>"") echo "<div>KAKOVOST SPREMEMBE: " . ($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name']) . "</div>";
    if (($sf_current_query['_sfm_starost']['active_terms'][0]['name'])<>"") echo "<div>STAROST: " . ($sf_current_query['_sfm_starost']['active_terms'][0]['name']) . "</div>";
    if (($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name'])<>"") echo "<div>VRSTA PRIMERA: " . ($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name']) . "</div>";
    }
    ?>

    And we know that the only part NOT working is:

    <div>Found <?php echo $sf_query->found_posts; ?> Results</div>
    
    #73970

    Anonymous
    Inactive

    Hey Trevor,

    Managed to figure it out.

    For the record here’s what I used (replacing 725 with whatever your specific search ID is..)

    <?php

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(725)->current_query();

    $array_data = $sf_current_query->get_array();
    $typeOfSearch = $array_data[“_sf_sort_order”][“active_terms”][0][“value”];

    echo $typeOfSearch ;

    ?>

    Keith

    #73321

    Anonymous
    Inactive

    Hi, we were able to get the meta values we needed with get_array();

    And for those curious the code we used was (square-feet is our custom field):

    <?php
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(315)->current_query();
    
    $array_data = $sf_current_query->get_array();
    $squarefeet_min = $array_data["_sfm_square-feet"]["active_terms"][0]["value"];
    $squarefeet_max = $array_data["_sfm_square-feet"]["active_terms"][1]["value"];
    ?>

    Thanks again for the help.

    #70909

    Anonymous
    Inactive

    function sf_filter_query_args( $query_args, $sfid ) {

    if($sfid==1225){

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(1225)->current_query();

    $sf_current_query->get_field_string(“_sfm_event_archives”);
    $_sfm_event_archives_array = $sf_current_query->get_array();
    $archive_is_check = $_sfm_event_archives_array[_sfm_event_archives][active_terms][0][value];

    //print_r($archive_is_check);

    if($archive_is_check != ‘1’){
    $args_custom = array(
    “meta_key” => “imic_event_start_dt”,
    “orderby” => “meta_value_num”,
    “order” => “ASC”,

    “meta_query” => Array (
    “event_archives” => Array (
    “key” => “event_archives”,
    “value” => “0”
    )
    )
    );

    $query_args = array_merge($query_args, $args_custom);
    }

    // si aucun filtre d’appliqué
    //if($sf_current_query->is_filtered()!=1){}

    }

    return $query_args;

    }
    add_filter( ‘sf_edit_query_args’, ‘sf_filter_query_args’, 100, 2 );

Viewing 10 results - 31 through 40 (of 50 total)