-
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-filter
sub-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.
October 5, 2020 at 11:34 am #261833In reply to: Determine is search active on Post Type Archive
TrevorParticipantYou can write some custom code to find the filter terms in use, but it does requires some digging to find the variable names first (especially if you allow more than once choice per field), and you can find the total results count. So, this code gets the count (but you need the query variable/array name, so it might not be $query):
$number_of_results = $query->found_posts;
To figure that the variables are, you need to output the array holding the filter, then make a note of the variable names. see this post:
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 3:58 pm #259944In reply to: Get active applied filters and search keyword
AnonymousInactiveHi Trevor,
I am not using a page builder or anything. I am using a CPT archive page and everything works fine. Even the results of “$searchandfilter->get(ID)->current_query()” are shown ON PAGE RELOAD which I’m trying to avoid since I want to load the results using AJAX.
Here is a screenshot of what I’m trying to achieve along with the use case:
https://share.getcloudapp.com/rRu7ZZzl– When a user selects a Year -> this would add an active filter pill with the selected year (example: 2020) – Same goes for the rest of the filters and the search input field.
– When a user hits the X on the active filter pill, it needs to remove that from the filters and resubmits the form.Thank you in advance!
September 17, 2020 at 3:33 pm #259937In reply to: Get active applied filters and search keyword
TrevorParticipantIf your results are being output by a page builder or grid plugin, this is not possible. Similarly, themes that are using frameworks, instead of traditional templates, you would need to use our Shortcode results method, which uses a PHP template.
This post explains a bit for you. Note, the code would have to go INSIDE the Ajax Container in the PHP template:
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!
August 25, 2020 at 7:37 pm #257288Topic: Get Field Options from Current Active Query
in forum Search & Filter Pro
AnonymousInactiveI’m working with the active query ($searchandfilter->get($SAFId)->current_query()) and I’m hoping to programmatically get all of the options for one of the fields. Is there a good way to go about this?
-
AuthorSearch Results
-
Search Results
-
I’m working with the active query ($searchandfilter->get($SAFId)->current_query()) and I’m hoping to programmatically get all of the options for one of the fields. Is there a good way to go about this?