Home › Forums › Backend Issues (wp-admin) › How can I send an additional field during ajax requests? › Reply To: How can I send an additional field during ajax requests?
@hube2 thanks a ton for your reply. Truth is I solved this earlier today, slightly differently than your proposed solution, but still it’s solved. I’ll post the code that I used, and I’ll elaborate on it later.
add_filter('acf/fields/post_object/result/name=screening_cinema', 'add_parent_to_cinema_options', 10, 4);
function add_parent_to_cinema_options($text, $post, $field, $post_id)
{
if ($post->post_parent) {
$text .= ' | ' . __('Parent', 'acf') . ': ' . get_the_title($post->post_parent);
}
return $text;
}
add_action('acf/input/admin_footer', 'add_js_for_disabling_cinema_options');
function add_js_for_disabling_cinema_options() {
?>
<script type = "text/javascript">
(function($) {
acf.add_filter('select2_ajax_results', function(json, params, instance) {
if (instance.data.field.data.name === 'screening_cinema') {
json.results.forEach(function(element) {
if (typeof element.description === "undefined") {
element.disabled = "true";
}
});
}
return json;
});
})(jQuery);
</script>
<?php
}
First of all, I noticed in includes/fields/class-acf-field-post_object.php at lines ~259-275 the following piece of code:
// vars
$result = array(
'id' => $id,
'text' => $text
);
// look for parent
$search = '| ' . __('Parent', 'acf') . ':';
$pos = strpos($text, $search);
if( $pos !== false ) {
$result['description'] = substr($text, $pos+2);
$result['text'] = substr($text, 0, $pos);
}
According to this, it’s possible to add a description and a text param in the json object used in acf.add_filter(‘select2_ajax_results’, function(json, params, instance)
That’s why I used the filter add_filter(‘acf/fields/post_object/result/name=screening_cinema’) to first add the parent part in the text shown, and then in the JS function I searched for the existence of the description param to decide whether I’ll disable the option or not.
Different approach than yours, the same effect and maybe a bit faster, as I’m not creating that whole bunch of top-category IDs. Plus, I liked that for the selected option, I get this | Parent: <parent title> in the dropdown so that I know exactly what parent the selection belongs to.
PS: The website is localized to my language that’s why you see that ” | Γονέας:” term in place of the ” | Parent:”.
Anyway, thanks a million for your help! Regards.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.