Forums › Forums › Search & Filter Pro › Filter without a (visible) form
- This topic has 74 replies, 3 voices, and was last updated 3 years, 10 months ago by Ross.
-
Anonymous(Private) December 22, 2020 at 6:02 pm #270801
Hi Trevor,
I know how to find the ID. That’s why that ID is in the code :P.
The problem, as described, is that nothing is happening.
The resultset is exactly the same as before, when it should be different.
If I do a var_dump() of $query_args; I get NULL, which I take to mean the if() statement is not being triggered.
The WPBakery post grid is set to connect to S&F, and I have chosen the form in the dropdown which matches to that form ID.
I assume the function is correct as the only thing I’ve changed is the
$sfid
and the respective values in$query_args['orientation'] = 'straight';
Why might the if() statement not be picking up the $sfid?
Trevor(Private) December 22, 2020 at 6:11 pm #270803Is the orientation field set in the form with that value selected? From what you are saying, no value has been selected. Thinking about it, that may be the issue. That function only works after a search has been done. Otherwise query_args is empty (null).
Anonymous(Private) December 23, 2020 at 9:35 am #270898OK, so the way S&F works isn’t compatible with the way I want the pages to load without creating a new form for each combination of location and orientation (every UK county & city x3, and then x4 further down the line).
Dangit. Heh. Please tell me there’s a fix for that coming in V3!
Time for a rethink.
So… I have a working html form with a dropdown/checkbox for location & orientation. This ties back to a single form ID on the back end. A copy of this form will sit at the top of each page.
When the page loads, those inputs come up with the default “all”/”unchecked” selected, as you’d expect.
In the results box below, every item in the resultset is displayed – as expected, as there are no filters.
I can get the appropriate filter term for the dropdown/checkbox from the slug using php.
Could I use JS to change the “selected” or “checked” value on the form inputs as appropriate, and would it automatically re-trigger the search to use the parameters I picked out using php? (assuming ajax loading and no “submit” button).
Ross Moderator(Private) December 23, 2020 at 1:35 pm #270938Hi Richard
I’m having a look through this ticket, and if I understand correctly, the first task we need to solve is how to alter the query / restrict things conditionally.
I think we were on the right path, with using the filter
sf_edit_query_args
but the implementation doesn’t look right from what I can see.So, to explain, our filter,
sf_edit_query_args
is just a fancy filter for editing query args that get passed into thenew WP_Query( $query_args );
we do for our queries (there is some other stuff going on, but that’s the core of it).What that means is, the structure of
$query_args
must match those of aWP_Query
– fortunately, something well documented here:
https://developer.wordpress.org/reference/classes/wp_query/What we need to understand first is, what type of data is orientation?
Is it custom field / post meta? If so, we can follow the instuctions here:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parametersAssuming your meta key is
orientation
then we can do something like:function filter_function_name( $query_args, $sfid ) { //if search form ID = 27601, the do something with this query if($sfid==27601) { //modify $query_args here before returning it $query_args['meta_key'] = 'orientation'; $query_args['meta_value'] = 'straight'; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Let me know how you get on with that!
Best
Anonymous(Private) December 29, 2020 at 12:56 am #271205Hi guys,
Not the most elegant solution, I suspect, but I have this code correctly pulling in the right values from the slug and parsing them. In the end I opted to filter by the category of the portfolio items as that tends to be more reliable than some of the other meta.
I’m not sure why, but even though the form is set up to return 42 results, the page only ever displays 10.
If I add
nopaging=>true,
to the query I get all results.Of course ideally I’d like to load them via ajax as the user scrolls. Do I need to add anything extra to this query, or pass any other variables to achieve that?
Many thanks!
The code is here:
function filter_function_name( $query_args, $sfid ) { global $post; $slug = $post->post_name; $sluglist = explode("-", $slug); $orientation = $sluglist[0]; unset($sluglist[0],$sluglist[1],$sluglist[2]); $location = implode("-", $sluglist); if($sfid=="28441"){ $query_args = array( 'post_type' => 'portfolio', 'portfolio_category' => "$orientation + $location" ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
-
AuthorPosts