I have two forms that I’d like to work together. The search form sits in the header and is only a search input (sfid 5729). The Filter form sits in the sidebar and has dropdowns for 3 custom taxonomies plus a from and to date (sfid 5727). They both are set to display in archive.php. The search form will use a url such as this, https://boardofednew.webdev.idaho.gov/search/?_sf_s=test.
The archive.php sidebar is implemented with content aware sidebars plugin and the sf widget for this filter form is set to display in it (5727).
So when I access the current press releases from the menu using url https://domainnamehere/resources/?_sft_resourcetype=press-releases and archive.php the filter form (5727) displays properly along with the filtered results for press releases. However, when I use the search form from the header (5729), it displays search results properly using archive.php, but it does not display the filter form (5757) in the sidebar. My intent is to return the search results for the term, then filter it further with the form in the sidebar. Code for archive.php follows:
‘
<?php
/**
*
* Description: Generic Index Page
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* If you’d like to further customize these archive views, you may create a
* new template file for each one. For example, tag.php (Tag archives),
* category.php (Category archives), author.php (Author archives), etc.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package FoundationPress
* @since FoundationPress 1.0.0
*/
?>
<?php get_header(); ?>
For anyone else with this issue please see my code below. Probably not perfect but it works. 🙂
// Exclude the default range values from the search as they are meaning the search returns no results
function remove_default_range( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==25 && $query_args) {
if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) {
global $searchandfilter;
$sf_current_query = $searchandfilter->get(25)->current_query()->get_array();
/* echo "<pre>";
print_r($sf_current_query);
echo "</pre>";*/
echo $sf_current_query['_sfm_minimum_per_round'][0]['value'];
if($sf_current_query['_sfm_minimum_per_round'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_round'][0]['value'] == '50000') {
unset($sf_current_query['_sfm_minimum_per_round']);
}
if($sf_current_query['_sfm_minimum_per_season'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_season'][0]['value'] == '1000000') {
unset($sf_current_query['_sfm_minimum_per_season']);
}
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'remove_default_range', 20, 2 );
I had a look, it seems to me there is something wrong with your code sample.
The filter receives a $query_args object, which contains the wp_query args that S&F defines, and at the end of the function it needs to return this $query_args object.
Inside the function you are free to modify this as you want, which it looks like you got close to do doing but not quite there..
You add your meta query to a new variable called $args, which never gets used.
I would do it like the following (untested – please also copy & paste into editor to see my comments, this is a bit clunky):
<?php
function filter_function_name( $query_args, $sfid ) {
if( $sfid == 940 ) {
// I would use some known values for testing (I just made some dates up here)
$tender_start = "20170101";
$tender_end = "20170101";
/*global $searchandfilter;
$sf_current_query = $searchandfilter->get(940)->current_query();
$array_data = $sf_current_query->get_array();
$tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value'];
$tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];*/
//if we want to play nice and not completely replace the existing meta query args
//there might be, then we need to check if some meta queries already exist and treat it differently
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tender_start',
'compare' => '>=',
'value' => $tender_start
),
array(
'key' => 'tender_end',
'compare' => '<=',
'value' => $tender_end
)
);
if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query'])))
{//this means there are some meta queries already here, so push this one onto the end
array_push($query_args['meta_query'], $meta_query);
}
else
{//else there aren't any meta queries
//$query_args['meta_query'] should always be an array of meta queries according to wp docs
$query_args['meta_query'] = array($meta_query);
}
// not sure if you need these, you should set the order via the S&F admin (in the <code>posts</code> tab)
$query_args['meta_key'] = 'tender_start';
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = 'ASC';
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I’ve written that in my editor so it should be correct syntax, but I haven’t actually tested it, its just showing you how to properly use the $query_args to make the changes you want.
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.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
*Other cookies used for logging in and cart functions will only be used when you use those features and cannot be disabled.
If you disable these cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!