Forums Forums Search Search Results for 'sf_current_query'

Viewing 10 results - 241 through 250 (of 318 total)
  • Author
    Search Results
  • #92504

    Trevor
    Participant

    MMM. If you DO select an author, and examine the $sf_current_query array, is the author data in there as a variable? Because you are not fetching from that array.

    #92230

    Anonymous
    Inactive

    Hi Trevor,

    In fact, i’ve a search.php file for my results. I wrote this code which appears on the top of the results :

    global $searchandfilter;
    global $post;
    	$author_id = $post->post_author;
    	$author = get_the_author_meta('display_name', $author_id);
    	$sf_current_query = $searchandfilter->get(802)->current_query();
    	$num_res = $wp_query->post_count;
    	echo $sf_current_query->get_field_string("_sft_category").'<br />';
    	echo $sf_current_query->get_field_string("_sft_thematique").'<br />';
            if($num_res == 0) {
    	echo 'Terme(s): '.$sf_current_query->get_search_term().'<br />';
    	}
    	echo 'El&eacute;ment(s) trouv&eacutes(s): ' .$wp_query->found_posts;

    I can display the author which is selected from the author select list but i don’t want to display anything if no author selected.

    I need a valid snippet which might be :

    if ($author_id != '0') {
    	echo $author.'<br />';
    	}

    Thank you for your help.

    Best regards.

    #92142

    Anonymous
    Inactive

    OK thats I got it.

    For anyone else who stumbles across this here’s the full code to output the Search Query

    Firstly this gets the Query (change number to your search form ID) – place in the loop in your template:

    global $searchandfilter;
      $sf_current_query = $searchandfilter->get(54)->current_query();
      $searchTerm = $sf_current_query->get_search_term();

    and then you can echo out the query in your anywhere in your template
    <?php echo $sf_current_query; ?>

    #92106

    Trevor
    Participant

    You need instead to use code like this to get the query:

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

    Where 339 is the form ID.

    You might need to dump the array to get an idea what is in it, but a search of the forum reveals some code snippets:

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

    I think it is possible that the next major release (V3, which we are working on right now) will have a much better way of doing this).

    #91601

    Anonymous
    Inactive

    Hello Trevor,
    What i meant : You give some snippets to display some informations like thais :

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(802)->current_query();
    echo $sf_current_query->get_field_string("_sft_category").'<br />';
    echo $sf_current_query->get_field_string("_sft_thematique").'<br />';
    echo 'Terme(s): '.$sf_current_query->get_search_term().'<br />';
    echo 'El&eacute;ment(s) trouv&eacutes(s): ' .$wp_query->found_posts;

    I’d like to have a snippet to display the author chosen in my select list. If i try that code, it doesn’t work :

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

    Is it possible to realize this ? It’s not in your documentation.

    Thank you for your help.

    Best regards.

    #91473

    Anonymous
    Inactive

    Ooops. I tried to create a code box with bbcode but it seems it doesn’t work here. Sorry for that! Here you have it again.

    <?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
     *
     */
    
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(21899)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      echo '<div> </div>'; 
    } else {
    	
    	if ( $query->have_posts() )
    {
    	?>
    
    	Se han encontrado <strong><?php echo $query->found_posts; ?> resultados</strong><br />
    	
    	<?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    		<div class='search-filter-results-list'>
    			<a href="<?php the_permalink(); ?>"><h2 class='search-filter-title'><?php the_title(); ?></h2>
    		
    			<?php 
    				if ( has_post_thumbnail() ) {
    					echo '<div class="menu-img">';
    					the_post_thumbnail("full");
    					echo '</div>';
    				}
    			?>			
    		</a></div>
    		
    		<?php
    	}
    	?>
    	<?php
    }
    else
    {
    	echo "No se han encontrado resultados";
    }
    }
    ?>
    #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 );
    #87657

    In reply to: Force selection?


    Trevor
    Participant

    Hi Lisa

    You can also let them search, but show them nothing (or a custom message) using this type of PHP code (change the number 1234 for the ID number of your search form and change the line echo '<div>Nothing to see here folks!</div>'; for whatever you want before the filter is used instead of the results – or remove for nothing at all):

    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 {
      // your theme template code here for the part that runs the loop, e.g. if ( $query->have_posts() ) .....
    }

    For example, if you were using the shortcode method. OK, if you have followed the documentation to make a copy of the results.php into a new folder called search-filter in your child theme or theme folder, then replace the contents of that template file with 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
     *
     */
    
    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 {
    	
    	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";
    }
    }
    ?>

    Trevor
    Participant

    Just in case this happens again, this is the code I wrote:

    <?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
     *
     */
    
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(5048)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      echo ''; 
    } 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>
    <div class="slz-shortcode sc_block_posts "><div class="slz-template-03 ">
    	<div class="slz-list-block slz-column-2">
    	<?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    		<div class="item">
    			<div class="slz-block-item-01 style-1">
    			<?php if ( has_post_thumbnail() ) {?>
    					<div class="block-image">
         <a href="<?php the_permalink(); ?>" class="link">
          <?php the_post_thumbnail("full", array( 'class' => 'img-responsive' ));?>
    					</a>
        </div>
    			<?php } ?>
        <div class="block-content">
         <div class="block-content-wrapper">
    			   <a class="block-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    						<ul class="block-info">
    							<li><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ); ?>"><span class="author-label">By </span><span class="author-text"><?php the_author(); ?></span></a></li>
    						 <li><a href="<?php the_permalink(); ?>" class="link date"><?php the_date(); ?></a></li>
    						 <li><a href="<?php the_permalink(); ?>#comments" class="link comment"><?php comments_number();?></a></li>
           <li><a href="<?php the_permalink(); ?>" class="link view">?? Views</a>
                            </li>
    						 <li><?php the_category(); ?></li>
    						</ul>
    						<div class="block-text"><?php echo get_the_excerpt(); ?></div>
    
    			  </div>
    		  </div>
    			
    			</div>
    		</div>
    		
    		<?php
    	}
    	?>
    	</div>
    	</div></div>
    
    	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";
    }
    }
    ?>
Viewing 10 results - 241 through 250 (of 318 total)