AnonymousInactive
I get the following (you can see a 1 at the end of the array):
Array
(
[_sft_project_category] => Array
(
[name] => Product Categories
[singular_name] => Product Category
[all_items_label] => All Categories
[type] => taxonomy
[active_terms] => Array
(
[0] => Array
(
[id] => 6
[name] => Fruit
[value] => fruit
[count] => 7
)
)
)
[_sft_project_tag] => Array
(
[name] => Product Tags
[singular_name] => Product Tag
[all_items_label] => All Tags
[type] => taxonomy
[active_terms] => Array
(
[0] => Array
(
[id] => 8
[name] => 6+ Months
[value] => 6-months
[count] => 11
)
)
)
)
1
AnonymousInactive
GOT IT!!!
global $searchandfilter;
$sf_current_query = $searchandfilter->get(504)->current_query()->get_array();
if (empty($sf_current_query)) {
echo ('All Articles');
}
else {
echo $sf_current_query['_sft_category']['active_terms'][0]['name'];
}
The above code worked like a charm, I also use the “Archive” setting as you suggested! Thanks so much for your help on this one!! Finally working properly!! 🙂
AnonymousInactive
Hi Trevor,
UPDATE! I’ve managed to half get it working!!!
It now works for all categories except the “All Categories” one, this seems to behave differently to the others and the code I have used doesn’t bring it in:
$sf_current_query = $searchandfilter->get(766)->current_query()->get_array();
echo $sf_current_query['_sft_category']['active_terms'][0]['name'];
I’ve left the printr statement in on the live site so you can get a picture of what is happening. I presume there may need to be an if statement added but I have yet to be able to pull in the “All categories” heading… I was however thinking of just hard coding a title in as it is only one title?? But i’m not sure how I would do it. I’m sure there is a “proper” fix to this though?
I hope you have a good weekend
AnonymousInactive
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(); ?>
<main id=”main” class=”body-content row column” role=”main”>
<?php get_template_part(‘template-parts/breadcrumbs’); ?>
<div class=”medium-8 columns”>
<header class=”page-header”>
<?php
global $searchandfilter;
$sf_current_query_1 = $searchandfilter->get(5279)->current_query(); // 5279 is search input only
$search_term = $sf_current_query_1->get_search_term();
$sf_current_query_2 = $searchandfilter->get(5277)->current_query(); // 5277 is custom taxonomy filter input only
$sarr = $sf_current_query_2->get_array();
$fromDate = $sarr[_sf_post_date][active_terms][0][value] ;
$toDate = $sarr[_sf_post_date][active_terms][1][value];
$type = $sf_current_query_2->get_field_string(“_sft_resourcetype”);
if ($type) {
$type = substr($type, strpos($type, “:”) + 1);
}
$topic = $sf_current_query_2->get_field_string(“_sft_primarytopic”);
if ($topic) {
$topic = substr($topic, strpos($topic, “:”) + 1);
}
$audience = $sf_current_query_2->get_field_string(“_sft_audience”);
if ($audience) {
$audience = substr($audience, strpos($audience, “:”) + 1);
}
if ($search_term) {
$type = “Search Results”;
}
echo ‘<h1>’ . $type . ‘</h1>’;
echo ‘<h2 class=”subheader”>’ . $topic . ” / “. $audience . ‘</h2>’;
echo ‘<p class=”small”>Note: Select date range or change resource, topic or audience as needed.</p>’;
?>
</header><!– .page-header –>
<?php if (have_posts()) : ?>
<section <?php post_class(); ?> >
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part( ‘template-parts/content’, get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( ‘template-parts/content’, ‘none’ ); ?>
</section>
<?php endif; // End have_posts() check. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( function_exists( ‘foundationpress_pagination’ ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
<nav id=”post-nav”>
<div class=”post-previous”><?php next_posts_link( __( ‘← Older posts’, ‘foundationpress’ ) ); ?></div>
<div class=”post-next”><?php previous_posts_link( __( ‘Newer posts →’, ‘foundationpress’ ) ); ?></div>
</nav>
<?php } ?>
</div>
<?php get_sidebar(); ?>
</main>
<?php get_footer(); ?>
‘
AnonymousInactive
I found some workaround, to display currently selected value:
$tmp = $sf_current_query->get_array();
echo $tmp["_sfm_character"]["active_terms"][0]["name"];
But it may be a bug in get_field_string method from documentation, which is not working in my case:
echo $sf_current_query->get_field_string("_sfm_character");
Hi Max
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.
Hope that helps
AnonymousInactive
Thank for your answer. Do you have a code sample? I tried some but no result.
function filter_function_name( $query_args, $sfid ) {
if( $sfid == 940 ) {
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'];
$args = array(
'meta_key' => 'tender_start',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'tender_start',
'compare' => '>=',
'value' => $tender_start
),
array(
'key' => 'tender_end',
'compare' => '<=',
'value' => $tender_end
)
)
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Marjan, we seem to have successfully coded up the solution for this, using a PHP snippet:
<?php
global $searchandfilter;
$sf_query = $searchandfilter->get(3217)->current_query();
if ($sf_query->is_filtered()) {
$sf_current_query = $searchandfilter->get(3217)->current_query()->get_array();
echo "<b>Prikaz rezultatov za...</b>";
$cat_string = "";
foreach($sf_current_query['_sft_category']['active_terms'] as $item) {
$cat_string .= $item['name'] . ", ";
}
$starost_string = "";
foreach($sf_current_query['_sfm_starost']['active_terms'] as $item) {
$starost_string .= $item['name'] . ", ";
}
if (($sf_current_query['_sft_category']['active_terms'][0]['name'])<>"") echo "<div>PRODUKTI: " . substr($cat_string, 0, -2) . "</div>";
if (($sf_current_query['_sft_post_tag']['active_terms'][0]['name'])<>"") echo "<div>STANJA: " . ($sf_current_query['_sft_post_tag']['active_terms'][0]['name']) . "</div>";
if (($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name'])<>"") echo "<div>KAKOVOST SPREMEMBE: " . ($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name']) . "</div>";
if (($sf_current_query['_sfm_starost']['active_terms'][0]['name'])<>"") echo "<div>STAROST: " . substr($starost_string, 0, -2) . "</div>";
if (($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name'])<>"") echo "<div>VRSTA PRIMERA: " . ($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name']) . "</div>";
}
?>
So I will close this now. Great to speak with you on Skype.
OK, nearly there. This is the code we have so far:
<?php
global $searchandfilter;
$sf_query = $searchandfilter->get(3217)->current_query();
if ($sf_query->is_filtered()) {
?>
<div>Found <?php echo $sf_query->found_posts; ?> Results</div>
<?php
$sf_current_query = $searchandfilter->get(3217)->current_query()->get_array();
if (($sf_current_query['_sft_category']['active_terms'][0]['name'])<>"") echo "<div>KATEGORIJE: " . ($sf_current_query['_sft_category']['active_terms'][0]['name']) . "</div>";
if (($sf_current_query['_sft_post_tag']['active_terms'][0]['name'])<>"") echo "<div>OZNAKE: " . ($sf_current_query['_sft_post_tag']['active_terms'][0]['name']) . "</div>";
if (($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name'])<>"") echo "<div>KAKOVOST SPREMEMBE: " . ($sf_current_query['_sfm_kakovost_spremembe']['active_terms'][0]['name']) . "</div>";
if (($sf_current_query['_sfm_starost']['active_terms'][0]['name'])<>"") echo "<div>STAROST: " . ($sf_current_query['_sfm_starost']['active_terms'][0]['name']) . "</div>";
if (($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name'])<>"") echo "<div>VRSTA PRIMERA: " . ($sf_current_query['_sfm_vrsta_primera']['active_terms'][0]['name']) . "</div>";
}
?>
And we know that the only part NOT working is:
<div>Found <?php echo $sf_query->found_posts; ?> Results</div>
AnonymousInactive
Well, that breaks the code, the part where the echo should be doens’t show anymore. The rest doesn’t show neither.
<?php global $searchandfilter;
$sf_current_query = $searchandfilter->get(15)->current_query()->get_array();
foreach($sf_current_query as $key) {
echo '<div>' . $key['name'] . '</div>';
echo '<div>' . $key['active_terms'][0]['name'] . '</div>';
} ?>