-
AuthorSearch Results
-
March 22, 2018 at 12:45 am #167512
In reply to: Multiple forms on one page + search analytics
AnonymousInactiveI had a few cracks but so far I haven’t been able to get it working. Here is what my current code looks like.
session_start(); add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 ); function filter_function_name( $query_args, $sfid ) { if($sfid==27339||$sfid==27361) { // Reset seed on load of initial archive page if( ! get_query_var( 'sf_paged' ) || get_query_var( 'sf_paged' ) == 0 || get_query_var( 'sf_paged' ) == 1 ) { if( isset( $_SESSION['seed'] ) ) { unset( $_SESSION['seed'] ); } } // Get seed from session variable if it exists $seed = false; if( isset( $_SESSION['seed'] ) ) { $seed = $_SESSION['seed']; } // Set new seed if none exists if ( ! $seed ) { $seed = rand(); $_SESSION['seed'] = $seed; } // Update ORDER BY clause to use seed $order_args = array( 'orderby' => 'RAND(' . $seed . ')' ); $query_args = wp_parse_args( $query_args, $order_args ); } return $query_args; }
January 26, 2018 at 11:13 am #155099In reply to: Search and Filter Pro not working with custom query
RossKeymasterHi Debbie
There are a couple of issues with the code.
You are setting up
$args
variable but returning$query_args
so your modifications are not being passed through.Also using
$args =
replaces all the data in the existing query, we don’t want to replace, but add your modification.Without checking if your date query actually works, my suggestion would be to modify the code like:
//Modify query args for filter UI on Productions archive template function filter_function_name( $query_args, $sfid ) { //if search form ID = 6128, the do something with this query if($sfid==6128) { //Today's date $today = date('Y-m-d H:i:s'); $meta_query = array( 'key' => 'event_date', 'value' => $today, 'compare' => '<=', ); $query_args['meta_query'] = array($meta_query); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Thanks
January 26, 2018 at 3:41 am #155080In reply to: Search and Filter Pro not working with custom query
AnonymousInactiveOk, I figured out a way to make it work, but I’m still running into one more issue.
I reset the search form to use display results as a post type archive.
I then created a separate page template that just displays upcoming productions, and am using the archives-productions.php template to display past productions, instead of the other way around.
The filter UI is showing now and the filter works. However, I only want to display past productions, and then filter through those using the plugin.
I tried using the edit query arguments filter, but I can’t seem to get it to work.
Here’s what I added to my functions.php file:
//Modify query args for filter UI on Productions archive template function filter_function_name( $query_args, $sfid ) { //if search form ID = 6128, the do something with this query if($sfid==6128) { //Today's date $today = date('Y-m-d H:i:s'); $args = array( 'meta_query' => array( array( 'key' => 'event_date', 'value' => $today, 'compare' => '<=', ), ) ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
November 2, 2017 at 2:47 pm #139846In reply to: Search & Filter with WPML
AnonymousInactiveThanks, Trevor. I tried adding the following to my functions.php but didn’t see the results I was looking for either.
function filter_function_name( $query_args, $sfid ) { //if search form ID = 100, then do something with this query if($sfid==261) { //modify $query_args here before returning it $query_args = array( 'post_type' => 'resource-item', 'suppress_filters' => true, 'orderby' => 'post_date', 'order' => 'DESC', 'tag__in' => array( 151, 348, 419 ), 'posts_per_page' => 10 ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I’ve been searching through the forum, and haven’t been able to find an example that includes the tags.
October 31, 2017 at 2:35 am #139351
AnonymousInactiveI solved this shortly after asking the question.
It was solved by placing the add_filter() calls to the posts_where and posts_join *AFTER* the “return $query_args;” line:
// REMOVE YOUTH SERVICE IMPROVEMENT GRANT PEOPLE function sf_filter_query_args( $query_args, $sfid ) { if(!is_library()) { return false; } $posts_to_exclude = array(); $args = array( 'posts_per_page' => -1, 'post_type' => 'grants', 'tax_query' => array( array( 'taxonomy' => 'program', 'field' => 'slug', 'terms' => array('youth-service-improvement-grants'), ), ), ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); $tags = get_the_terms($post->ID, 'person'); if(!empty($tags)) { foreach($tags as $tag) { $tag_slug = $tag->slug; $tag_terms = array($tag_slug); $args = array( 'posts_per_page' => -1, 'post_type' => 'grants', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'person', 'field' => 'slug', 'terms' => $tag_terms ), array( 'taxonomy' => 'program', 'field' => 'slug', 'terms' => array('youth-service-improvement-grants'), 'operator' => 'NOT IN' ), ) ); $my_person_posts = get_posts( $args ); if(count($my_person_posts) < 1) { $myperson = get_page_by_path( $tag_slug, OBJECT, 'people' ); if(!empty($myperson)) { $posts_to_exclude[] = $myperson->ID; } } } } endforeach; wp_reset_postdata(); $query_args['post__not_in'] = $posts_to_exclude; return $query_args; add_filter('posts_where','atom_search_where'); add_filter('posts_join', 'atom_search_join'); } add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 200);
October 25, 2017 at 11:50 am #138398
RossKeymasterHi there
Just taking a look at this.
The reason we implemented the
edit_query_args
filter is exactly for this reason, the arguments get passed through to our count functions too, so these should be updated, and its why this filter should be used over the WPpre_get_posts
…So, this is odd.
2 things I would suggest:
1) We are only trying to edit the
$query_args
(more specifically the meta query), not replace all of them (by using=
on the variable you replace anything in the array already), perhaps this is causing an issue. I would change your code above to:function filter_upcoming_events( $query_args, $sfid ) { if($sfid==29642) { $query_args["meta_query"] = array( "key" => "event_date_time", "value" => time() - (60 * 60 * 2), "compare" => ">=" ), "meta_key" => "event_date_time", "order" => "ASC", "orderby" => "meta_value", ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );
I would also make sure, that any Post Meta settings, defined in the settings form, are removed / disabled, as well as the order results option in
posts
tab in your search form (this is all just to be safe and ensure no conflicts).2) You could have a
pre_get_posts
somewhere else in your setup, causing this unusual behaviour, but if using the post meta settings in the form works fine (with the counts) then this may not be the issue. Lets see about option 1 first.Let me know how you get on.
Thanks
October 19, 2017 at 10:21 pm #137453In reply to: Filter content by current user
AnonymousInactiveHi Trevor,
a quick update, just in case someone is looking for a solution. The following filter works for me,
only posts written by currently logged-in user will be displayed.function filter_function_name( $query_args, $sfid ) { //if search form ID = 100, then do something with this query if($sfid==100) { //modify $query_args here before returning it $query_args = array( 'post_type' => 'my_custom_post_type', 'author' => $user->ID ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Thanks,
Georg
October 17, 2017 at 11:59 am #136903
AnonymousInactiveThanks for that, I’ve got it working now but the count on the filters are wrong. The list of filters is now including all posts rather than the ones that match the new query?
This wasn’t a problem when I used the Post Meta conditions through the plugin UI
This is the function I have up to now…
function filter_upcoming_events( $query_args, $sfid ) { if($sfid==29642) { $query_args = array( "meta_query" => array( "key" => "event_date_time", "value" => time() - (60 * 60 * 2), "compare" => ">=" ), "meta_key" => "event_date_time", "order" => "ASC", "orderby" => "meta_value", ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );
Page is at http://www.our-pub.co.uk/wp
As you can see, the England games under the Football filters have already been played so the England filter should be hidden as it’s empty. ticking the England box and submitting the search returns no results.
Thanks in advance.
October 13, 2017 at 2:16 pm #136461
AnonymousInactiveThis solved my problem:
https://gist.github.com/rmorse/074db89682afa8501b15
function sf_filter_query_args( $query_args, $sfid ) { $posts_to_exclude = array(); $args = array( 'posts_per_page' => -1, 'post_type' => 'grants', 'tax_query' => array( array( 'taxonomy' => 'program', 'field' => 'slug', 'terms' => array('youth-service-improvement-grants'), ), ), ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); $tags = get_the_terms($post->ID, 'person'); if(!empty($tags)) { foreach($tags as $tag) { $tag_slug = $tag->slug; $tag_terms = array($tag_slug); $args = array( 'posts_per_page' => -1, 'post_type' => 'grants', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'person', 'field' => 'slug', 'terms' => $tag_terms ), array( 'taxonomy' => 'program', 'field' => 'slug', 'terms' => array('youth-service-improvement-grants'), 'operator' => 'NOT IN' ), ) ); $my_person_posts = get_posts( $args ); if(count($my_person_posts) < 1) { $myperson = get_page_by_path( $tag_slug, OBJECT, 'people' ); if(!empty($myperson)) { $posts_to_exclude[] = $myperson->ID; } } } } endforeach; wp_reset_postdata(); $query_args['post__not_in'] = $posts_to_exclude; return $query_args; } add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
October 6, 2017 at 10:56 am #135093In reply to: Filter content by current user
AnonymousInactiveHi Trevor,
thanks for pointing me to Edit Query Arguments Filter, must have missed that!
Here is how I set things up:
– a front end form for submitting private custom posts (cpt supports author)
– search & filter restricted to
– post type: my custom post type
– post status: privateI expected search & filter to take into account that limitation to private actually limits search results to current users posts, as a regular wordpress query would do. But maybe my setup is wrong.
If s&f doesn’t work this way, consider this a feature request. Because otherwise it renders wordpress post status private useless.
Next, I tried your approach of editing Arguments Filter, but it gives me no results. Probably wrong syntax?
function filter_user_posts( $query_args, $sfid ) { $user_id = get_current_user_id(); //if search form ID = 109, do something with this query if($sfid==109) { //modify $query_args here before returning it $query_args = array( 'author' => $user_id ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_user_posts', 20, 2 );
Speaking of security issues: I will further investigate the URL-snooping issue. But I also have a content restrictions on single post view in other places, so posts can’t be viewed by non-authors anyway.
Cheers,
Georg
-
AuthorSearch Results