-
AuthorSearch Results
-
April 5, 2016 at 9:54 am #41634
In reply to: Turn off / Ignore Results per page setting
RossKeymasterThere shouldn’t be a custom query inside the filter itself, the filter is used to modify the arguments before they get passed to a query – the code should look like:
function results_map_filter( $query_args, $sfid ) { //if search form ID = 1530, the do something with this query if($sfid==1530) { //modify $query_args here before returning it $query_args['posts_per_page'] = -1; } return $query_args; } add_filter( 'sf_edit_query_args', 'results_map_filter', 10, 20 );
Thanks
April 5, 2016 at 8:57 am #41626In reply to: Turn off / Ignore Results per page setting
TrevorParticipantI have no idea if your code is roughly correct, but it is not semantically correct.
It would look like this to be correct from a PHP point of view, I think:
<?php function results_map_filter( $results_map_args, $sfid ) { if($sfid==1530) { $results_map = get_posts( $results_map_args ); if (have_posts() ) { $results_map_args = array( 'post_type' => 'posttype', 'posts_per_page' => -1, 'post_status' => 'publish', 'cache_results' => true, 'no_found_rows' => true ); } return $results_map_args; } add_filter( 'sf_edit_query_args', 'results_map_filter', 10, 2 ); ?>
You would use this in your child theme functions.php file, which should already have the opening PHP tag
<?php
, so you wouldn’t need that from the code, and it is NOT normal to close the PHP tags?>
at the end of the functions.php file as this can cause a site to crash.I could well be wrong with the code though.
April 5, 2016 at 8:44 am #41625In reply to: Turn off / Ignore Results per page setting
AnonymousInactiveCan you please explain more. I have this code now but the number of results don’t change
<?php function results_map_filter( $results_map_args, $sfid ) {
if($sfid==1530) {
$results_map_args = array(
‘post_type’ => ‘posttype’,
‘posts_per_page’ => -1,
‘post_status’ => ‘publish’,
‘cache_results’ => true,
‘no_found_rows’ => true
);
}
return $results_map_args;
}
add_filter( ‘sf_edit_query_args’, ‘results_map_filter’, 10, 2 );
$results_map = get_posts( $results_map_args );
if (have_posts() ) :
?>April 3, 2016 at 1:41 pm #41395In reply to: Adding Post ID's to returned results
RossKeymasterHey Matt
This is pretty tricky actually.
Since the introduction of the cache, most queries are sent off to the cache DB, and they return a number of IDs… which are placed inside the
post__in
field in a query.So, I’m actually scratching my head as to how we could do this.
There is a filter for modifying the query:
add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );
Which should be used like:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==225) { //modify $query_args here before returning it $query_args['post__in'] = array(1,2,3,4); } return $query_args; }
Which S&F also uses for setting up the query – and will probbably overeride this withe the results from the S&F cache.
Try initializing hook much later (so S&F has finished with the post__in var):
add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 200 );
Thanks
March 14, 2016 at 5:20 pm #39398
RossKeymasterHey Paul
You don’t need to do this via query args – you can do all this via the
posts
tab.If it must be dynamic and via the query args, then its the same as when using the standard
new WP_Query($args)
So, to modify
orderby
you need this part of the WP docs:https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
Updating the S&F filter example with the example from the WP site on order:
$args = array( 'orderby' => 'title', 'order' => 'DESC' ); $query = new WP_Query( $args );
Will update the filter like:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==225) { //modify $query_args here before returning it $query_args['orderby'] = 'title'; $query_args['order'] = 'DESC'; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );
Thanks
March 7, 2016 at 10:25 pm #38917In reply to: Search Box
RossKeymasterHi there
If you must edit the query best not to hack the plugin directly 😉
Instead, you can use the filter
sf_edit_query_args
– which allows you to change any of the parameters that are passed to the query:If you lower the priority to say 20:
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
This will happen after S&F has setup the query.
This means you should be able to access the object like:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==225) { //modify $query_args here before returning it //here you can limit the length of 's' which is the search term $query_args['s'] = "overwritten search term"; } return $query_args; }
Thanks
February 17, 2016 at 6:40 pm #37242In reply to: Filter Query Args
AnonymousInactiveRoss,
I just tested this and found that if I display results using the search.php then the filter can’t use the get_queried_object()->name; , but if I display results using the shortcode method within the same taxonomy template, the get_queried_object()->name; is usable for the query filter.Here’s what I currently have:
function member_query_args( $query_args, $sfid ) { $current_member_category = get_queried_object()->name; if($sfid==3906) { $query_args = array( 'member-category' => $current_member_category ); } return $query_args; } add_filter( 'sf_edit_query_args', 'member_query_args', 10, 2 );
February 16, 2016 at 11:48 pm #37151In reply to: Filter Query Args
AnonymousInactiveOK,
I’m able to get this to filter:function member_query_args( $query_args, $sfid ) {
if($sfid==3906) {
$query_args = array(
‘member-category’ => ‘hotels-motels’
);
}
return $query_args;
}How can I go about dynamically placing the value of member-category ?
add_filter( ‘sf_edit_query_args’, ‘member_query_args’, 10, 2 );
February 16, 2016 at 9:34 pm #37123Topic: Filter Query Args
in forum Search & Filter Pro
AnonymousInactiveHello Support,
I’m trying to filter search results based on a taxonomy item. I’m trying to this code that I found in the forum, but I can’t get it to work even just using $query_args[‘order’] = ‘desc’;What am I doing wrong here? Below is the the code I’ve been trying.
function sf_filter_query_args( $query_args, $sfid ) {
if($sfid==3906) {
$query_args[‘member-category’] = get_queried_object()->name;
//$query_args[‘order’] = ‘desc’;
}
return $query_args;
}add_filter( ‘sf_edit_query_args’, ‘sf_filter_query_args’, 10, 2 );
November 3, 2015 at 4:23 pm #28855In reply to: Add existing parameters of query to shortcode
RossKeymasterHi Sergey
Its just the same as the
$args
object you pass to a newWP_Query
– so this link should provide you with the necessary options:https://codex.wordpress.org/Class_Reference/WP_Query
To take their category example and apply it to the S&F filter:
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters=
<?php function sf_filter_query_args( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==225) { //modify $query_args here before returning it //only show results within categories with these IDs $query_args['cat'] = '2,6,17,38'; } return $query_args; } add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 ); ?>
Thanks
-
AuthorSearch Results
-
Search Results
-
Topic: Filter Query Args
Hello Support,
I’m trying to filter search results based on a taxonomy item. I’m trying to this code that I found in the forum, but I can’t get it to work even just using $query_args[‘order’] = ‘desc’;What am I doing wrong here? Below is the the code I’ve been trying.
function sf_filter_query_args( $query_args, $sfid ) {
if($sfid==3906) {
$query_args[‘member-category’] = get_queried_object()->name;
//$query_args[‘order’] = ‘desc’;
}
return $query_args;
}add_filter( ‘sf_edit_query_args’, ‘sf_filter_query_args’, 10, 2 );