The forums are closed and will be removed when we launch our new site.
Looking for support? You can access the support system via your account.
-
AuthorSearch Results
-
October 12, 2020 at 4:09 pm #262732
Christiaan PunterParticipantAlas, that results in a Syntax Error, preventing the code to be added:
“Your PHP code changes were rolled back due to an error on line 1082 of file wp-content/themes/theblog/functions.php. Please fix and try saving again.”
syntax error, unexpected ‘||’ (T_BOOLEAN_OR)
FYI: line 1082 holds the following line of your code: if ($sf_current_query->is_filtered())||($sf_current_query->get_search_term()<>””)||is_page(25640)) {
October 12, 2020 at 3:46 pm #262728
TrevorModeratorTry this (not sure this will work):
add_action('related_post_main','related_post_main_remove_hook_sandf', 0); function related_post_main_remove_hook_sandf() { global $searchandfilter; $sf_current_query = $searchandfilter->get(25639)->current_query(); if ($sf_current_query->is_filtered())||($sf_current_query->get_search_term()<>"")||is_page(25640)) { remove_action('related_post_main','related_post_main_title'); remove_action('related_post_main','related_post_main_post_loop'); remove_action('related_post_main','related_post_main_css'); remove_action('related_post_main','related_post_main_slider_scripts'); } }
October 7, 2020 at 5:44 pm #262294In reply to: “?” Being stripped out of the results URL
Danny AlbeckParticipantThis reply has been marked as private.October 7, 2020 at 3:47 pm #262240In reply to: Don’t Show Results Until User Clicks Submit/Search
TrevorModeratorAre 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
TrevorModeratorYou 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
TrevorModeratorIf 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
OdiParticipantHi 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
OdiParticipantHi 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
TrevorModeratorIf 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
OdiParticipantI 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?
-
AuthorSearch Results