-
AuthorSearch Results
-
September 17, 2020 at 5:26 pm #259956
In reply to: Get active applied filters and search keyword
AnonymousInactiveHi Trevor,
Thank you for the response. The code you sent works well, however, it needs to be in the archive page itself to work, which is something I’m trying to avoid because that would mean the page has to reload to display the active filters.I tried to run it through AJAX once the form submits. But I’m not getting any results. (below is the function I added to functions.php) (the ajax request works fine, so the issue is with $searchandfilter).
Is there any way to use the “$searchandfilter” class and the “current_query()” method from OUTSIDE the archive page?
Please let me know. thank you.
add_action( 'wp_ajax_show_active_filters', 'show_active_filters' ); add_action( 'wp_ajax_nopriv_show_active_filters', 'show_active_filters' ); function show_active_filters(){ global $searchandfilter; $sf_current_query = $searchandfilter->get(8175)->current_query()->get_array(); ob_start(); echo '<span class="active-pill">'.$sf_current_query['_sft_contest-years']['active_terms'][0]['name'].'</span>'; $ajax_filters_html .= ob_get_contents(); ob_end_clean(); echo json_encode(array('html'=>$ajax_filters_html)); wp_die(); }
July 17, 2020 at 9:56 am #253147In reply to: Show Dropdown Selection in Page Title
TrevorParticipantThe code you would need is:
global $searchandfilter; $sf_current_query = $searchandfilter->get(9490)->current_query()->get_array(); echo $sf_current_query['_sfm_UnitLocation']['active_terms'][0]['name'];
Look carefully, as only the ‘global’ line is the same.
Note, if you are using Ajax on the page, you may need to make sure that the page title is inside the Ajax Container, otherwise it will not refresh and change.
July 7, 2020 at 12:30 pm #251712
TrevorParticipantMore complex is needed then.
<?php global $searchandfilter; $sf_current_query = $searchandfilter->get(1234)->current_query()->get_array(); $search_cat = $sf_current_query['_sft_category']['active_terms'][0]['name']; ?> <header class="searсh-header"> <h1 class="page-title">Résultats de la recherche : <span><?php echo $search_cat;?></span></h1></header>
June 23, 2020 at 5:44 am #249704In reply to: ACF / Post Meta Fields in current_query() Result
TrevorParticipantYou first need to find out where the data is, and to do so you need to examine the filter array:
<?php global $searchandfilter; $sf_current_query = $searchandfilter->get(1526)->current_query()->get_array(); echo '<pre>',print_r($sf_current_query,true),'</pre>'; ?>
This will print that array to the screen. From that you would need to use code LIKE this:
echo $sf_current_query['_sft_category']['active_terms'][0]['name']
This would echo out the first Category term. You will need to write a similar line, for example:
echo $sf_current_query['_sfm_industries']['active_terms'][0]['name']
June 2, 2020 at 2:56 pm #246960In reply to: Echo custom field name on results page
TrevorParticipantI 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.
May 29, 2020 at 6:03 pm #246641In reply to: Echo custom field name on results page
AnonymousInactiveI 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"; } ?>
May 29, 2020 at 5:33 pm #246628In reply to: Echo custom field name on results page
TrevorParticipantAh. 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?
May 29, 2020 at 4:57 pm #246600In reply to: Echo custom field name on results page
TrevorParticipant_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.
March 31, 2020 at 2:21 pm #238475In reply to: Page Title For Search Results
TrevorParticipantSo, here is a search:
_sft_location=cotswolds
_sft_partysize=ten-guests
_sft_features=garden-patio,internetThe 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>
March 19, 2020 at 4:26 pm #237221
TrevorParticipantYou seem to have customised the results.php file, but it must have errors as it also shows ‘no results found’?
This post might help you with code for what you want:
That code is for _sft_category and the id number is wrong, so maybe this?
global $searchandfilter; $sf_current_query = $searchandfilter->get(3637)->current_query(); $sf_current_query_array = $searchandfilter->get(3637)->current_query()->get_array(); if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { echo '<div>No category selected.</div>'; } else { echo '<div>' . $sf_current_query_array["_sft_menucats"]["active_terms"][0]["value"] . '</div>'; echo '<div>' . $sf_current_query_array["_sft_menucats"]["active_terms"][0]["id"]. '</div>'; echo '<div>' . category_description($sf_current_query_array["_sft_menucats"]["active_terms"][0]["id"]). '</div>'; }
Otherwise, are you able to send me temporary WordPress admin logins (ones you should later delete or disable) for the site so that I can look at the setup? Please use a Private Reply.
-
AuthorSearch Results