Forums › Forums › Search & Filter Pro › Pass right post id in ACF hook
- This topic has 44 replies, 3 voices, and was last updated 4 years, 5 months ago by Ross.
-
Anonymous(Private) June 26, 2020 at 9:50 am #250212
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!
Trevor(Private) June 26, 2020 at 10:54 am #250229Thinking 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?
Anonymous(Private) June 26, 2020 at 12:30 pm #250256Hi Trevor, thanks again for your help, and thanks for improving my code readability 🙂
It is indeed being added to the form on the options in a form field. I don’t understand how I get get the id, because there could be any and multiple taxonomy (product group) values (tires, seats, lights, etc) on the page; I only place your shortcode for the filter and your plugin handles that the right filters are being shown. My idea is that the right taxonomy id should therefore be passed from you plugin?
As a test, on a product page, I added
get_queried_object_id()
to get the taxonomy id, and hard coded that as an extra argument in the hook. This has no effect; the instructions are still randomly being echoed or not. I think as a side effect it tells that this is the id to use.I think I have to ask ACF with these new insights, but if you have any idea I would like to hear it.
Thanks,
RoelandAnonymous(Private) June 29, 2020 at 10:38 am #250388Hi 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?
Ross Moderator(Private) June 29, 2020 at 12:51 pm #250447Hi Roeland
I think if you use any other display method than shortcode (such as custom, and make your own WP_Query as per here – https://searchandfilter.com/documentation/search-results/custom/ )
Then all requests, even ajax, are frontend – in fact they stay within the same page, so it should all work in theory.
Want to give that a test?
Thanks
Anonymous(Private) June 29, 2020 at 2:26 pm #250469I will sure like to give that a test. So I have to build a custom query, get the current taxonomy and add it as an argument too, and loop over that new query, right? Just asking for sure because it is quite some work. And how do I, in that case, show the filter, if it can’t been done with a shortcode?
-
AuthorPosts