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 - 91 through 100 (of 115 total)
  • Author
    Search Results
  • #104843

    mhair
    Participant

    I have two forms that I’d like to work together. The search form sits in the header and is only a search input (sfid 5729). The Filter form sits in the sidebar and has dropdowns for 3 custom taxonomies plus a from and to date (sfid 5727). They both are set to display in archive.php. The search form will use a url such as this, https://boardofednew.webdev.idaho.gov/search/?_sf_s=test.

    The archive.php sidebar is implemented with content aware sidebars plugin and the sf widget for this filter form is set to display in it (5727).

    So when I access the current press releases from the menu using url https://domainnamehere/resources/?_sft_resourcetype=press-releases and archive.php the filter form (5727) displays properly along with the filtered results for press releases. However, when I use the search form from the header (5729), it displays search results properly using archive.php, but it does not display the filter form (5757) in the sidebar. My intent is to return the search results for the term, then filter it further with the form in the sidebar. Code for archive.php follows:


    <?php
    /**
    *
    * Description: Generic Index Page
    *
    * Used to display archive-type pages if nothing more specific matches a query.
    * For example, puts together date-based pages if no date.php file exists.
    *
    * If you’d like to further customize these archive views, you may create a
    * new template file for each one. For example, tag.php (Tag archives),
    * category.php (Category archives), author.php (Author archives), etc.
    *
    * @link https://codex.wordpress.org/Template_Hierarchy
    *
    * @package FoundationPress
    * @since FoundationPress 1.0.0
    */
    ?>
    <?php get_header(); ?>

    <main id=”main” class=”body-content row column” role=”main”>
    <?php get_template_part(‘template-parts/breadcrumbs’); ?>
    <div class=”medium-8 columns”>
    <header class=”page-header”>
    <?php
    global $searchandfilter;
    $sf_current_query_1 = $searchandfilter->get(5279)->current_query(); // 5279 is search input only
    $search_term = $sf_current_query_1->get_search_term();
    $sf_current_query_2 = $searchandfilter->get(5277)->current_query(); // 5277 is custom taxonomy filter input only
    $sarr = $sf_current_query_2->get_array();
    $fromDate = $sarr[_sf_post_date][active_terms][0][value] ;
    $toDate = $sarr[_sf_post_date][active_terms][1][value];
    $type = $sf_current_query_2->get_field_string(“_sft_resourcetype”);
    if ($type) {
    $type = substr($type, strpos($type, “:”) + 1);
    }
    $topic = $sf_current_query_2->get_field_string(“_sft_primarytopic”);
    if ($topic) {
    $topic = substr($topic, strpos($topic, “:”) + 1);
    }
    $audience = $sf_current_query_2->get_field_string(“_sft_audience”);
    if ($audience) {
    $audience = substr($audience, strpos($audience, “:”) + 1);
    }
    if ($search_term) {
    $type = “Search Results”;
    }
    echo ‘<h1>’ . $type . ‘</h1>’;
    echo ‘<h2 class=”subheader”>’ . $topic . ” / “. $audience . ‘</h2>’;
    echo ‘<p class=”small”>Note: Select date range or change resource, topic or audience as needed.</p>’;
    ?>
    </header><!– .page-header –>
    <?php if (have_posts()) : ?>
    <section <?php post_class(); ?> >
    <?php while (have_posts()) : the_post(); ?>
    <?php get_template_part( ‘template-parts/content’, get_post_format() ); ?>
    <?php endwhile; ?>
    <?php else : ?>
    <?php get_template_part( ‘template-parts/content’, ‘none’ ); ?>
    </section>
    <?php endif; // End have_posts() check. ?>
    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( function_exists( ‘foundationpress_pagination’ ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
    <nav id=”post-nav”>
    <div class=”post-previous”><?php next_posts_link( __( ‘← Older posts’, ‘foundationpress’ ) ); ?></div>
    <div class=”post-next”><?php previous_posts_link( __( ‘Newer posts →’, ‘foundationpress’ ) ); ?></div>
    </nav>
    <?php } ?>
    </div>
    <?php get_sidebar(); ?>
    </main>
    <?php get_footer(); ?>

    #98807

    Joe Mallion
    Participant

    Wooo, that got it. Thank you for your patience.

    For anyone else with this issue please see my code below. Probably not perfect but it works. 🙂

    // Exclude the default range values from the search as they are meaning the search returns no results
    
    function remove_default_range( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==25 && $query_args)	{
    		if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) {
    			global $searchandfilter;
    			$sf_current_query = $searchandfilter->get(25)->current_query()->get_array();
    			/* echo "<pre>";
    			print_r($sf_current_query);
    			echo "</pre>";*/
    
    			echo $sf_current_query['_sfm_minimum_per_round'][0]['value'];
    
    			if($sf_current_query['_sfm_minimum_per_round'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_round'][0]['value'] == '50000') {
    				unset($sf_current_query['_sfm_minimum_per_round']);
    			}
    
    			if($sf_current_query['_sfm_minimum_per_season'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_season'][0]['value'] == '1000000') {
    				unset($sf_current_query['_sfm_minimum_per_season']);
    			}
    
    		}			
    	}
    
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'remove_default_range', 20, 2 );
    #98769

    Trevor
    Moderator

    It is a while since I looked at the array, but I think I did something like this:

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(25)->current_query()->get_array();
    echo "<pre>";
    print_r($sf_current_query);
    echo "</pre>";
    #95975

    ydoron
    Participant

    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


    Max
    Participant

    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
    Moderator

    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
    Moderator

    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

    Keith Commins
    Participant

    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

    Evan
    Participant

    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.

Viewing 10 results - 91 through 100 (of 115 total)