Forums Forums Search Search Results for 'sf_current_query'

Viewing 10 results - 51 through 60 (of 318 total)
  • Author
    Search Results
  • #249201

    Trevor
    Participant

    Can you try adding this code to the page, so you can see what the query is actually sending, if anything (it might actually give you the variable to use as well):

    <?php
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(102003)->current_query()->get_array();
    echo '<pre>',print_r($sf_current_query,true),'</pre>';
    ?>

    Please note that we are now closed for the day as it is late afternoon here in the UK, so my next reply may not be until tomorrow.


    Anonymous
    Inactive

    Hello,

    We have set up a page with S&F with the display results method ‘Using a shortcode’.

    Before the shortcode is executed, I would like to get the first result of the query (I want to display the first item as a highlight before the complete results).

    I thought I could get the results via:

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

    But it seems there are no results in this object.

    Is there a way to retrieve the first item of a query from the shortcode method?

    Thanks in advance!
    -Kevin

    #246960

    Trevor
    Participant

    I have edited and tested it for you. This is the code I used in the end:

    	global $searchandfilter;
    	$sf_current_query = $searchandfilter->get(1949)->current_query()->get_array();
    	$category = $sf_current_query['_sft_category']['active_terms'][0]["name"];
    	$county = $sf_current_query["_sfm_county"]["active_terms"][0]["name"];
    	if ($category) {
    		echo $category . ", ";
    	} else {
    		echo "All Categories, ";
    	}
    	if ($county) {
    		echo $county;
    	} else {
    		echo "All Counties";
    	}

    You may edit that to make it look better though.

    #246641

    Anonymous
    Inactive

    I do, thanks. However, it’s now outputting the text ‘ArrayAntigonish’ (or ‘Array’ + whatever county is selected) and it’s not outputting category name. I have tried to puzzle through this to see if I can work out how to fix it myself but my PHP just isn’t good enough.

    Here’s a link to a sample results page.

    What I’d like it to say is something like “You searched for Breweries, Distilleries, and Vineyards in Antigonish County” and then the results.

    Below is the full code of results.php. If you can offer any more insight I’d be grateful.

    <?php
    /**
     * Search & Filter Pro 
     *
     * Sample Results Template
     * 
     * @package   Search_Filter
     * @author    Ross Morsali
     * @link      https://searchandfilter.com
     * @copyright 2018 Search & Filter
     * 
     * Note: these templates are not full page templates, rather 
     * just an encaspulation of the your results loop which should
     * be inserted in to other pages by using a shortcode - think 
     * of it as a template part
     * 
     * This template is an absolute base example showing you what
     * you can do, for more customisation see the WordPress docs 
     * and using template tags - 
     * 
     * http://codex.wordpress.org/Template_Tags
     *
     */
    
    // If this file is called directly, abort.
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( $query->have_posts() )
    {
    	?>
    	
    	There are <?php echo $query->found_posts; ?> posts that match your search! <br />
    
    	<?php
    	
    	 global $searchandfilter;
    	 
    $sf_current_query = $searchandfilter->get(1949)->current_query()->get_array();
    
    echo implode(", ",$sf_current_query['_sft_category']['active_terms']);
    $county = $sf_current_query["_sfm_county"]["active_terms"][0]["value"];
    if ($county) {
    echo $county;
    } else {
    echo "All Counties";
    }
    	
    
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    		<div>
    			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    			
    			<p><?php the_excerpt(); ?></p>
    			
    			
    		</div>
    		
    		<hr />
    		<?php
    	}
    	?>
    	Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
    	
    	<div class="pagination">
    		
    		<div class="nav-previous"><?php next_posts_link( 'Next page', $query->max_num_pages ); ?></div>
    		<div class="nav-next"><?php previous_posts_link( 'Previous page' ); ?></div>
    		<?php
    			/* example code for using the wp_pagenavi plugin */
    			if (function_exists('wp_pagenavi'))
    			{
    				echo "<br />";
    				wp_pagenavi( array( 'query' => $query ) );
    			}
    		?>
    	</div>
    	<?php
    }
    else
    {
    	echo "No Results Found";
    }
    ?>
    #246628

    Trevor
    Participant

    Ah. Try this then:

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(1949)->current_query()->get_array();
    
    echo implode(", ",$sf_current_query['_sft_category']['active_terms']);
    $county = $sf_current_query["_sfm_county"]["active_terms"][0]["value"];
    if ($county) {
    echo $county;
    } else {
    echo "All Counties";
    }

    I am coding a bit blind here though. Notice how the second line changed, and the way the category output is changed?

    #246600

    Trevor
    Participant

    _sft_meta_value, would, I think, be _sfm_county

    So, you would echo something like this:

    echo $sf_current_query->get_fields_html(array("_sft_category"));
    echo $sf_current_query["_sfm_county"]["active_terms"][0]["value"];

    With County, that code assumes you have just one term selected. If none are selected, you might have first to test if that has a value. Like this:

    echo $sf_current_query->get_fields_html(array("_sft_category"));
    $county = $sf_current_query["_sfm_county"]["active_terms"][0]["value"];
    if ($county) {
    echo $county;
    } else {
    echo "All Counties";
    }

    If you allow more than one county, it would get a little more complicated, but not much.

    #245870

    Trevor
    Participant

    You have chosen to use for now our Shortcode display results method, which means that the style of the display is somewhat basic, but can be customized by yourself:

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

    It depends what content you want (what taxonomies, images, description, custom fields) and layout you want, as that will determine how easy it will be to do.

    #1 This code:

    Found <?php echo $query->found_posts; ?> Results<br />
    Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />

    Would become this:

    <div style="text-align: right">Found <?php echo $query->found_posts; ?> Results, Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?></div>

    #2 The above code would then become:

    <div style="text-align: right">
    <?php global $searchandfilter;
    $sf_current_query = $searchandfilter->get(13622)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { } else { ?>
    Found <?php echo $query->found_posts; ?> Results, 
    <?php } ?>
    Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?></div>

    #3 This space?

    https://www.screencast.com/t/GuYOFQ3qtQ3l

    There appears to be blank lines in your content:

    https://www.screencast.com/t/SivDBdT69fK

    #4 This would require our filter to use (or be able to use) transients, which it cannot at present. I believe this will be added when we release V3, towards the end of June/early July.

    Even then, you would probably require the services of a coder to hook it all up.

    #242521

    Trevor
    Participant

    I am assuming that you are using the WooCommerce method? You could edit the PHP template being used and add some logic to test if a filter has yet been applied, like this (change the ID to match the form you are using):

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(1234)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      echo '<div>Nothing to see here folks!</div>'; 
    } else {
      // the current products output code here
    }

    Trevor
    Participant

    You need to modify the code in the results.php to this:

    <?php
    /**
     * Search & Filter Pro 
     *
     * Sample Results Template
     * 
     * @package   Search_Filter
     * @author    Ross Morsali
     * @link      http://www.designsandcode.com/
     * @copyright 2015 Designs & Code
     * 
     * Note: these templates are not full page templates, rather 
     * just an encaspulation of the your results loop which should
     * be inserted in to other pages by using a shortcode - think 
     * of it as a template part
     * 
     * This template is an absolute base example showing you what
     * you can do, for more customisation see the WordPress docs 
     * and using template tags - 
     * 
     * http://codex.wordpress.org/Template_Tags
     *
     */
    
    // If this file is called directly, abort.
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(70)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      echo '<div>Nothing to see here folks!</div>'; 
    } else {
    	
    	if ( $query->have_posts() )
    {
    	?>
    	
    	Found <?php echo $query->found_posts; ?> Results<br />
    	Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
    	
    	<div class="pagination">
    		
    		<div class="nav-previous"><?php next_posts_link( 'Older posts', $query->max_num_pages ); ?></div>
    		<div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></div>
    		<?php
    			/* example code for using the wp_pagenavi plugin */
    			if (function_exists('wp_pagenavi'))
    			{
    				echo "<br />";
    				wp_pagenavi( array( 'query' => $query ) );
    			}
    		?>
    	</div>
    	
    	<?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    		<div>
    			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    			
    			<p><br /><?php the_excerpt(); ?></p>
    			<?php 
    				if ( has_post_thumbnail() ) {
    					echo '<p>';
    					the_post_thumbnail("small");
    					echo '</p>';
    				}
    			?>
    			<p><?php the_category(); ?></p>
    			<p><?php the_tags(); ?></p>
    			<p><small><?php the_date(); ?></small></p>
    			
    		</div>
    		
    		<hr />
    		<?php
    	}
    	?>
    	Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
    	
    	<div class="pagination">
    		
    		<div class="nav-previous"><?php next_posts_link( 'Older posts', $query->max_num_pages ); ?></div>
    		<div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></div>
    		<?php
    			/* example code for using the wp_pagenavi plugin */
    			if (function_exists('wp_pagenavi'))
    			{
    				echo "<br />";
    				wp_pagenavi( array( 'query' => $query ) );
    			}
    		?>
    	</div>
    	<?php
    }
    else
    {
    	echo "No Results Found";
    }
    }
    ?>
    #238475

    Trevor
    Participant

    So, here is a search:

    _sft_location=cotswolds
    _sft_partysize=ten-guests
    _sft_features=garden-patio,internet

    The code I added was this (replace the 12345 with your form ID):

    <div>
      <?php
      global $searchandfilter;
      $sf_current_query = $searchandfilter->get(12345)->current_query()->get_array();
      $locations_filter = $sf_current_query["_sft_location"]["active_terms"][0]["name"];
      if ( $locations_filter <> "") {
        echo '<div>Location: ' . $locations_filter . '</div>';
      }
      $party_size_filter = $sf_current_query["_sft_partysize"]["active_terms"][0]["name"];
      if ( $party_size_filter <> "") {
        echo '<div>Guests: ' . $party_size_filter . '</div>';
      }
      $features_filter_array = $sf_current_query["_sft_features"]["active_terms"];
      if ( $features_filter_array ) {
        echo '<div>Features: ';
        foreach($features_filter_array as $value) {
          $features_list .= $value["name"] . ", ";
        }
        echo rtrim($features_list, ", ") . '</div>';
        }
      ?>
    </div>
Viewing 10 results - 51 through 60 (of 318 total)