Search Results for 'current_query'

Forums › Forums › Search › Search Results for 'current_query'

Viewing 10 results – 31 through 40 (of 385 total)
  • Author
    Search Results
  • #259571

    Anonymous
    Inactive

    Literally 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!

    #259569

    Anonymous
    Inactive

    Hi 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!

    #257288

    Anonymous
    Inactive

    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?

    #256885

    In reply to: No search results


    Trevor
    Participant

    I 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.php file you can edit, in a sub-folder named search-filter in 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>
    
    #256352

    Trevor
    Participant

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

    #256340

    Trevor
    Participant

    The 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
    }
    #256329

    Anonymous
    Inactive

    Amazing, this works perfectly, thank you!!

    I’m using it to run a function to offset the number of posts on the first page, but only if the page is outputting the default/unfiltered loop. Is this the best way to check that?

    global $searchandfilter;
    $filtered = $searchandfilter->get(1045)->current_query()->is_filtered();
    if ( !is_admin() && $query->is_main_query() && is_home() && $filtered != 1 ) {
    ...
    }
    #256219

    Anonymous
    Inactive

    Hello,

    So I have 2 S&F forms on my pages (different results pages). Is it possible to pass the query from my main text search to a sidebar filters S&F form I have? The filters on the left live on results page from the main search bar, and if you try to use them they reset the query from the text search (which makes sense technically). I am wondering if there is a way that if a user hits ones of those filters I can pass the query from the other S&F form?

    I saw this:

    function filter_function_name( $query_args, $sfid ) {

    if ( is_search() ) {
    //var_dump($sf_current_query);
    }

    //if search form ID = 225, the do something with this query
    if($sfid==225)
    {
    //modify $query_args here before returning it
    $query_args[‘somearg’] = ‘newvalue’;
    }

    return $query_args;
    }
    add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );

    But wasn’t sure if that was the right path to grab the current query and pass it along to the other form.

    Thank you!

    #255240

    Trevor
    Participant

    The PHP to use to conditionally run code looks like this (change the ID of the form to suit):

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(1234)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      // the current (unfiltered) posts output code here
    } else {
      // the desired (filtered) posts output code here
    }
    #254727

    In reply to: Results count


    Anonymous
    Inactive

    Trevor,

    It works.
    $sf_current_query = $searchandfilter->get($id)->current_query();
    $term = $sf_current_query->get_search_term();
    $count = $wp_query->found_posts;

    Thank you,

    Christian

Viewing 10 results – 31 through 40 (of 385 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.