-
AuthorSearch Results
-
October 7, 2020 at 3:47 pm #262240
In reply to: Don’t Show Results Until User Clicks Submit/Search
TrevorParticipantAre you able to send me a live link/URL to your search page so I can take a look?
Which Display Results Method are you using? With some, it cannot be done, with those using a PHP template, it can, with some coding. For example, using our Shortcode method …
… in which case you should follow the ‘guide to customising’:
Once you have a copy of the results.php file in a
search-filtersub-folder of your theme, you can edit that file, like this (leave the PHP comments at the top outside and before this code):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 resuts.php code here }Where the ID number in this needs to match your form ID (replace 1234). Similar code can be used in your own PHP template file.
September 21, 2020 at 11:58 am #260128In reply to: Post grid results display
TrevorParticipantIf you mean that you want to only show results if the form has been used, this is not possible using the Post Grid plugin, as it has no option to stop the results showing. I suppose you could be sneaky and place the Post Grid shortcode inside a PHP snippet shortcode, where you can then use PHP to detect if the filter has been used. That might work. You would need this plugin:
https://wordpress.org/plugins/post-snippets/
Then use that to create a shortcode that contains PHP, like this:
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 { do_shortcode('[searchandfilter id="1234" action="filter_next_query"]'); do_shortcode('[post_grid id="5678"]'); }Replace the ID numbers to suit.
Then place the shortcode for that snippet in the page instead of the two you have inside it. That might work. For Ajax to work, I think you might need to place the shortcode from the snippet plugin inside a div with its own class name, and use that for the Ajax Container.
September 17, 2020 at 5:26 pm #259956In 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(); }September 17, 2020 at 2:52 pm #259932In reply to: Get active applied filters and search keyword
AnonymousInactiveI found this class that allows to output the current filters
<?php //Get an array of objects containing data for the current search/filter //replace <code>1526</code> with the ID of your search form global $searchandfilter; $sf_current_query = $searchandfilter->get(1526)->current_query(); var_dump($sf_current_query->get_array()); ?>But what I’m really looking for is to dynamically display the filter terms AND search term on Submit (AJAX enabled). Also the ability to Clear those Filters / Search items one by one (X them out).
Please let me know if this is doable?
September 17, 2020 at 6:13 am #259839In reply to: The reset button is always there
TrevorParticipantAh. I guess it works, but I suspect it would not in ajax mode?
If you have access to the PHP template file that is the one that outputs the while loop (inside the products container), you could add some PHP and use that to include some CSS to hide the reset button, like this:
<?php global $searchandfilter; $sf_current_query = $searchandfilter->get(1234)->current_query(); if (!($sf_current_query->get_search_term()=="") { <? <style> .woof_submit_search_form_container {display: none;} </style> <?php } ?>Note: you would need to change the ID number, and it looks like you don’t have a search box, so this should work.
BTW, I think you need to add style like this also (generally):
.woof_redraw_zone {display: flex;}This will stop content from below bleeding on to the ‘menu/form’.
September 16, 2020 at 2:33 am #259571In reply to: Can’t Get a Results Count with Oxygen Builder
AnonymousInactiveLiterally figured this out directly after posting this. I’ll post my solution for anyone interested.
Here is the code I used in a PHP block to display the search term and a post count:
global $searchandfilter; $sf_current_query = $searchandfilter->get(678)->current_query(); echo $sf_current_query->get_search_term(); $the_query = new WP_Query(array( 'post_type' => 'courses', )); echo $the_query->found_posts; wp_reset_postdata();Both before this code block and before the repeater that follows I put this shortcode (so twice):
[searchandfilter id=”YOURSEARCHID” action=”filter_next_query”]I had tried this almost exactly last night (except without any arguments for $the_query) but it didn’t work. The trick was that I had to filter “$the_query” by at least one argument, in this case post_type. If I left the arguments empty or passed an empty $args, the searchandfilter shortcode wouldn’t filter. I’m assuming that shortcode is just a pre_get_posts based on the filter parameters and there needs to be at least one parameter for pre_get_posts to work.
Either way, I believe I have it figured out – thanks for making a great plugin! It would be great to get more direct Oxygen Builder support in the future as it’s an amazing builder for people who can code but in the meantime, things are working so I’m happy!
September 16, 2020 at 2:01 am #259569Topic: Can’t Get a Results Count with Oxygen Builder
in forum Search & Filter Pro
AnonymousInactiveHi there,
I just purchased the pro version and was able to get nearly everything to work properly with Oxygen Builder using the guide posted by Jan Langhans here:
https://support.searchandfilter.com/forums/topic/search-and-filter-pro-oxygen-builder-repeater/
I was even able to make it work with an Oxygen Archive template even though that post said it wasn’t possible (although I still have to enter the page url for where the template will appear).
Additionally, I was able to display the current search result by using a PHP block and using this code:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(678)->current_query();
echo $sf_current_query->get_search_term();I wrote out everything I can do just to show why it is so frustrating that I can’t figure out how to simply display the total results count after being filtered by Search and Filter Pro. I tried echo $sf_current_query->found_posts; directly after the code I have above and that comes back as null and displays nothing. I tried creating a new wp query and using found_posts with that but that just displays the total count unfiltered by search and filter. I have a feeling there’s some simple way to access the total results like the get_search_term() function above but I just can’t figure it out.
Any help would be greatly appreciated!
August 21, 2020 at 10:42 am #256885In reply to: No search results
TrevorParticipantI apologise for the delay Jens, I ran out of time yesterday.
If you haven’t already done so, can you follow this guide:
https://searchandfilter.com/documentation/search-results/using-a-shortcode/
In particular, follow the instructions in the Customising section.
That will give you a
results.phpfile you can edit, in a sub-folder namedsearch-filterin your theme folder.Would you want to show that after you search? My guess is yes. So, look for this code in the results.php template file:
Found <?php echo $query->found_posts; ?> Results<br /> Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />And change that to (I think this will work):
global $searchandfilter; $sf_current_query = $searchandfilter->get(30408)->current_query(); if (!((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()==""))) { Found <?php echo $query->found_posts; ?> Results<br /> Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br /> }You can change anything else you want also. For example, to make the pagination look nicer, install the free WP-PageNavi plugin and delete these lines:
<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>August 18, 2020 at 8:56 am #256352In reply to: Query as a breadcrumb or similar
TrevorParticipantYou would need to code some PHP. As you are using the Shortcode display results method, start with this guide (if you have not already):
https://searchandfilter.com/documentation/search-results/using-a-shortcode/#customising-the-results
To fetch the search terms, the https://searchandfilter.com/documentation/accessing-search-data/ guide is basic but you can extend the idea to grab lots of other data. If you have other filters, then it becomes a little more complex, but I can give you links. This thread might help you:
https://support.searchandfilter.com/forums/topic/accessing-field-slug-on-search-results/
… and this search will give similar threads I think:
https://support.searchandfilter.com/forums/search/sf_current_query+get_array
Note, if you are using Ajax refreshing of the results, any PHP needs to be inside the Ajax Container, or it will not update.
August 18, 2020 at 6:45 am #256340In reply to: is_paged() function no longer works
TrevorParticipantThe code we would normally recommend, so you may need to modify this, or may indeed have used this, is:
global $searchandfilter; $sf_current_query = $searchandfilter->get(1045)->current_query(); if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { // the unfiltered posts output code here } else { // the current (filtered) posts output code here } -
AuthorSearch Results