Forums Forums Search & Filter Pro Echo custom field name on results page

Viewing 10 posts - 1 through 10 (of 12 total)
  • Anonymous
    #246480

    On NovaScotiaFood.com I have a search form that uses AND as an operator. The user simply selects a category of food producer and a county, and they get a list of that type of food producer in the selected county. The search works fine. The results are not yet displaying the way I’d like.

    I’ve just copied some of your example code on my results page:

    <?php
    	//Get a multiple fields values by passing an array of field names
    	//replace <code>1526</code> with the ID of your search form
    	global $searchandfilter;
    	$sf_current_query = $searchandfilter->get(1949)->current_query();
    
    	/*
    	 * EXAMPLE 1 
    	 */
    
    	echo $sf_current_query->get_fields_html(
    		array("_sft_category", "_sft_meta_value")
    	);

    where I just tried to guess that ‘_sft_meta_value’ would output the custom field entry that was searched for, but I guessed wrong. What should I be putting in there instead to correctly output the custom field? Thanks.

    Trevor
    #246489

    Are you able to send me a live link/URL to your search page so I can take a look?

    Anonymous
    #246504

    Search form is in a widget called “Search by county” on any page but the front one.

    Example:

    Results page:

    https://novascotiafood.com/search-results/

    Trevor
    #246600

    _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.

    Anonymous
    #246624

    I popped in the first two lines of PHP you recommended but it throws this error for the second line:

    Fatal error: Uncaught Error: Cannot use object of type Search_Filter_Active_Query as array in /home/willi413/public_html/novascotiafood.com/wp-content/themes/nova-scotia-food/search-filter/results.php:51 Stack trace: #0 /home/willi413/public_html/novascotiafood.com/wp-content/plugins/search-filter-pro/public/includes/class-search-filter-query.php(1148): include() #1 /home/willi413/public_html/novascotiafood.com/wp-content/plugins/search-filter-pro/public/includes/class-search-filter-display-results.php(48): Search_Filter_Query->the_results() #2 /home/willi413/public_html/novascotiafood.com/wp-content/plugins/search-filter-pro/public/includes/class-search-filter-display-shortcode.php(884): Search_Filter_Display_Results->output_results(1949, Array) #3 /home/willi413/public_html/novascotiafood.com/wp-includes/shortcodes.php(343): Search_Filter_Display_Shortcode->display_shortcode(Array, ”, ‘searchandfilter’) #4 [internal function]: do_shortcode_tag(Array) #5 /home/willi413/public_html/novascotiafood.com/wp-includes/shortcod in /home/willi413/public_html/novascotiafood.com/wp-content/themes/nova-scotia-food/search-filter/results.php on line 51

    Trevor
    #246628

    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?

    Anonymous
    #246641

    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";
    }
    ?>
    Trevor
    #246643
    This reply has been marked as private.
    Anonymous
    #246649
    This reply has been marked as private.
    Trevor
    #246960

    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.

Viewing 10 posts - 1 through 10 (of 12 total)