Ah, sorry, I just realized, you want the category to show, NOT the search term?
That may be a little more complex. This MIGHT work:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1234)->current_query();
$search_cat = $sf_current_query->get_field_string("_sft_category");
?>
<header class="searсh-header">
<h1 class="page-title">Résultats de la recherche : <span><?php echo $search_cat;?></span></h1></header>
You want to display the content of these fields in the results? If you are using the Shortcode display results method then you need to follow the customising guide here:
https://searchandfilter.com/documentation/search-results/using-a-shortcode/#customising-the-results
There are elements that you do not need, so you can delete those. For ACF fields, you can output the contents of the fields to the page using PHP code like this (the example is for a field named test_price
):
$test_price = get_field('test_price');
if( $test_price ) echo $test_price;
Your actual PHP may need to add HTML around what is echoed to the page.
AnonymousInactive
This strikes me as an easy fix but i’m just not finding the method –
I’m writing a filter results page title like so:
<h1 class="entry-title"><?php echo $sf_current_query->get_field_string("_sft_rugtype"); ?></h1>
with _sft_rugtype (aka, Rug Type:) being the taxonomy we’re filtering by, this all works fine but the title gets printed as “Rug Type: [term name]” – we’re looking to exclude the taxonomy in the display so it only says the term name. My apologies if this is documented somewhere i’m struggling to find it.
AnonymousInactive
Hi Ross,
Thanks for checking!
The result in your screenshot is recently added bij ACF as a test, and always working indeed because they hardcoded a field name and a post id in it:
var_dump(get_field_object('#!#etrtodiameter','314322'));
When you look at the html attribute “title” from the filter “ISO diameter” below your screenshot, and you refresh several times, you will see that it is sometimes filled and sometimes not. This attribute is being picked up by this hook in functions.php:
‘
function filter_function_name($input_object, $sfid)
{
$acf_name = substr($input_object[‘name’],5);
//echo ‘ACF-name: ‘ . $acf_name . ‘. ‘;
$field = get_field_object($acf_name); // As a test I added 14313 as a product taxonomy id (2nd parameter), this seems to have no effect (still randomly echoing instructions or not)
$instruction = $field[‘instructions’];
//echo ‘instruction: ‘ . $instruction . ‘. ‘;
//echo ‘field_object: ‘ . get_field_object($acf_name) . ‘. ‘;
if(strlen($instruction)>0)
{
$input_object[‘attributes’][‘title’] = $instruction;
}
return $input_object;
}
add_filter(‘sf_input_object_pre’, ‘filter_function_name’, 10, 2);
‘
So the question is, how can the stability of the first code snippet be integrated in the second code snippet, while keeping it dynamic; custom fields can be related to multiple taxonomy id’s and post id’s and I never know what products are being shown. Or, while typing, I wonder if a solution could be to pick the first product in a filter and pass the id as and argument for the function in the second code snippet. What do you think? And if this is a good idea, do you have an idea how to build this efficiently?
Thanks too for your suggestion about the shortcode/front end loading issue. How can I achieve this when using a custom, dynamically used, template?
Thinking about the code. Change this:
$instruction = get_field_object($acf_name)['instructions'];
To this:
$field = get_field_object($acf_name);
$instruction = $field['instructions'];
That just makes it neater to read.
The field is stored where though? It is not stored in a post, is it? If not, what is it attached to?
This is being added to the form, on the options in a form field?
If so, then this:
$field = get_field_object($acf_name, $tax_term_id);
$instruction = $field['instructions'];
Where you have to first find the ID number of the taxonomy term. Is that already an attribute in our array?
AnonymousInactive
Hi Trevor,
The hook that you helped me with this week, for adding ACF-fields to filters is working randomly; sometimes it returns the wanted info, sometimes it is empty. I contacted ACF about it and this is their (first) answer: “since you are on a taxonomy archive, I believe passing a second parameter containing a post id would be very helpful to ensure that the get_field_object always retrieves data from this post.”.
I think in that case that I have to pass the post id from the filter, or am I wrong? Do you have a suggestion how to achieve this?
The hook:
function filter_function_name($input_object, $sfid)
{
$acf_name = substr($input_object['name'],5);
//echo 'ACF-name: ' . $acf_name . '. ';
$instruction = get_field_object($acf_name)['instructions'];
//echo 'instruction: ' . $instruction . '. ';
//echo 'field_object: ' . get_field_object($acf_name) . '. ';
if(strlen($instruction)>0)
{
$input_object['attributes']['title'] = $instruction;
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
Thanks!
AnonymousInactive
Ok nice, this is working:
function filter_function_name($input_object, $sfid)
{
$acf_name = substr($input_object['name'],5);
if(strlen($acf_name)>0)
{
$input_object['attributes']['title'] = get_field_object($acf_name)['instructions'];
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);
It only gives different attribute names based on the field type, for example “title” and “class title” but I wil fix that with JS.
Thanks a lot!
AnonymousInactive
My S&F is entirely filtering on ACF fields as post meta results.
I’d like to display the selected search terms, however there seems to be a difference in how to get these vs more standard fields like category.
For example:
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1526)->current_query();
echo $sf_current_query->get_field_string("_sft_category");
gives the expected result, but changing the argument in the third line to be a custom field using the name that appears in the url bar(?_sfm_work_type=26-%2C-27&_sfm_industries=14-%2C-15) doesn’t give any results, even though the filter works fine.
global $searchandfilter;
$sf_current_query = $searchandfilter->get(1526)->current_query();
echo $sf_current_query->get_field_string("_sfm_industries");
Is there a change in method to get those fields?
AnonymousInactive
<?php
/**
* Template name: Freelancers page
*
* @package Freelancer
*/
get_header();
?>
<?php echo do_shortcode( '[elementor-template id="499"]');?>
<section class="container-flex">
<div class="left-column">
<h3>Categorieën</h3>
<?php echo do_shortcode('[searchandfilter id="73"]' );?>
</div>
<div class="right-column">
<div class="header-right-column">
<h1><i class="fa fa-handshake-o" aria-hidden="true"></i>Freelancers - Ready To Work</h1>
</div>
<?php
$currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'freelancer',
'search_filter_id'=> 73,
'posts_per_page' => 3,
'paged'=>$currentPage
);
$query = new WP_Query($args);
if($query->have_posts()): ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="post-inhoud">
<div class="flex-box">
<div class="box-left">
<?php if( get_field('profielfoto') ): ?>
<img />" />
<?php endif; ?>
</div>
<div class="box-right">
<h3><?php the_title(); ?></h3>
<h4 class=""><?php the_field('functie'); ?></h4>
</div>
</div><!-- end of .flex-box-->
<?php the_category(); ?>
<p class="meta"><i class="fa fa-map-marker" aria-hidden="true"></i><?php the_field('locatie_freelancer'); ?> <i class="fa fa-money" aria-hidden="true"></i>€ <?php the_field('uurprijs'); ?>,- per uur
<i class="fa fa-calendar-o" aria-hidden="true"></i>Beschikbaar per week: <?php the_field('beschikbaarheid'); ?> uur</p>
<p><?php $summary = get_field('omschrijving_vaardigheden');
echo substr($summary, 0, 150); ?><a>"> ....lees verder.. </a></p><br>
<a>"><button type="button">Naar profiel <i class="fa fa-chevron-right" aria-hidden="true"></i></button></a>
</div><!--end of .post-inhoud-->
<?php endwhile; ?>
<?php else : echo "<p class='geen-zoekresultaat'>Er zijn geen zoekresultaten!</p><br>"; ?>
<?php
$GLOBALS['wp_query']->max_num_pages = $query->max_num_pages;
the_posts_pagination( array(
'mid_size' => 3,
'prev_text' => __( 'Vorige pagina', 'freelancer' ),
'next_text' => __( 'Volgende pagina', 'freelancer' ),
'screen_reader_text' => __( 'Posts navigation', 'freelancer' )
) ); ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div><!--end of .right-column-->
</section>
<?php
get_sidebar();
get_footer();
AnonymousInactive
The code got all mixed up, so here I have posted it again
I have couple of issues
See this page
https://acu2020dev.wpengine.com/story
I have created a page template and added the shortcodes
echo do_shortcode('[searchandfilter id="133"]');
echo do_shortcode('[searchandfilter id="133" show="results"]');
I am showing a featured post first and then I am showing the listing
As I dont want to show the featured post I added the following code
function filter_function_name( $query_args, $sfid ) {
//echo “Coming here”;
//if search form ID = 225, the do something with this query
if($sfid==133)
{
//modify $query_args here before returning it
if (is_page_template(‘page-templates/stories.php’)) {
//echo “This is “.get_queried_object_id();
$featured_story = get_field(“featured_story_landingpage”,get_queried_object_id());
$exclude = array($featured_story->ID);
$query_args[‘post__not_in’] = $exclude;
}
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );
This works alright when the page is loaded, but when I click the Clear filter button or when a category is selected, the above exclusion is ignored and the featured post is shown in the listing
What am I doing wrong? or is this a bug in the plugin?
Also how can I show a numbered pagination?
regards