大家早上好,我是新来的, 我快被这个问题搞疯了。 我需要从 func 传递变量 $sel 。 “我的行动”进入 函数“get_players”。 该变量始终会被求值,但在“get_players”函数中该变量始终为空。 有人能帮我吗? 谢谢你。
function my_action() {
global $selezionato;
$selezionato = isset($_POST['selezionato']) ? $_POST['selezionato'] : '';
echo $selezionato; //here is evaluated and works
wp_die();
}
add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');
function get_players($field) {
global $selezionato;
echo $selezionato; // IS ALWAYS NULL !!
$field['choices'] = array();
$args = array(
'numberposts' => -1,
'post_type' => 'players',
'meta_key' => 'club',
'meta_value' => $selezionato // IS ALWAYS NULL !!
// 'orderby' => 'title',
// 'order' => 'ASC'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
// global $post;
while ($the_query->have_posts()) {
$the_query->the_post();
$value = get_field('numero');
$label = get_the_title();
// append to choices
$field['choices'][$value] = $label;
}
wp_reset_postdata();
}
return $field;
}
add_filter('acf/load_field/name=marcatore-casa', 'get_players');
我尝试将 $selezionato 变量设置为全局变量,并且我希望将 var $selezionato 的值传递给“get_players”函数
使用钩子“update_option()”将变量值存储在选项中,如下所示:
function my_action() {
$selezionato = isset($_POST['selezionato']) ? $_POST['selezionato'] : '';
echo $selezionato; // here is evaluated and works
update_option('my_action_selezionato', $selezionato); // Store the value in an option
wp_die();
}
add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');
使用钩子“get_option()”检索 get_players 函数中变量的值,如下所示:
function get_players($field) {
$sel = get_option('my_action_selezionato'); // Retrieve the value from the option
echo $sel; // Should now display the correct value
// Rest of your code...
return $field;
}
add_filter('acf/load_field/name=marcatore-casa', 'get_players');