Wordpress:在 function.php 中使用 WP_query() 和高级自定义字段
Wordpress: using WP_query() and Advanced Custom Fields in function.php
我被主题卡住了。我编写了一个函数,它获取自定义类型 posts 的某些字段('advanced custom fields' 插件用于字段),将其推送到数组中,将其编码为 json 并更新 .json 文件。
function opt_update() {
$args = array(
'post_type' => 'opt'
);
$opt_array = array();
$the_query = new WP_Query($args);
while ($the_query -> have_posts()): $the_query -> the_post();
$good_type_array = array();
while (have_rows('type')): the_row();
$type_name = get_sub_field('type_name');
array_push($good_type_array, $type_name);
endwhile;
array_push($opt_array, array(
'name' => get_the_title(),
'good_type' => $good_type_array,
'price' => get_field('price')
));
endwhile;
wp_reset_postdata();
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/blablabla/json/data.json', raw_json_encode($opt_array));
}
function raw_json_encode($input) {
return preg_replace_callback(
'/\\u([0-9a-zA-Z]{4})/',
function ($matches) {
return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
},
json_encode($input)
);
}
如果我调用该函数,它会完美运行,例如,在主页上:
opt_update();
但我需要的是在我publish/update 'opt' 自定义post 时执行函数。我尝试使用那个钩子:
add_action('publish_opt', 'opt_update', 10, 2);
...但它根本不起作用。虽然这不是钩子的问题,但如果我用另一个函数代替 'opt_update',例如:
add_action('publish_opt', 'mail_me', 10, 2);
...它按预期向我发送了一封电子邮件。
我哪里错了?
UPD:尝试使用通用的 wordpress posts 和更简单的代码来完成...仅在我第二次按下 'update' 按钮后仍然写入文件
function opt_update($ID, $post) {
$args = array(
'post_type' => 'post'
);
$opt_array = array();
$the_query = new WP_Query($args);
while ($the_query -> have_posts()): $the_query -> the_post();
array_push($opt_array, array(
'price' => get_field('price')
));
endwhile;
wp_reset_postdata();
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/blablabla/json/data.json', raw_json_encode($opt_array));
}
add_action('publish_post', 'opt_update', 10, 2);
acf/save_post 钩子就可以了
我被主题卡住了。我编写了一个函数,它获取自定义类型 posts 的某些字段('advanced custom fields' 插件用于字段),将其推送到数组中,将其编码为 json 并更新 .json 文件。
function opt_update() {
$args = array(
'post_type' => 'opt'
);
$opt_array = array();
$the_query = new WP_Query($args);
while ($the_query -> have_posts()): $the_query -> the_post();
$good_type_array = array();
while (have_rows('type')): the_row();
$type_name = get_sub_field('type_name');
array_push($good_type_array, $type_name);
endwhile;
array_push($opt_array, array(
'name' => get_the_title(),
'good_type' => $good_type_array,
'price' => get_field('price')
));
endwhile;
wp_reset_postdata();
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/blablabla/json/data.json', raw_json_encode($opt_array));
}
function raw_json_encode($input) {
return preg_replace_callback(
'/\\u([0-9a-zA-Z]{4})/',
function ($matches) {
return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
},
json_encode($input)
);
}
如果我调用该函数,它会完美运行,例如,在主页上:
opt_update();
但我需要的是在我publish/update 'opt' 自定义post 时执行函数。我尝试使用那个钩子:
add_action('publish_opt', 'opt_update', 10, 2);
...但它根本不起作用。虽然这不是钩子的问题,但如果我用另一个函数代替 'opt_update',例如:
add_action('publish_opt', 'mail_me', 10, 2);
...它按预期向我发送了一封电子邮件。 我哪里错了?
UPD:尝试使用通用的 wordpress posts 和更简单的代码来完成...仅在我第二次按下 'update' 按钮后仍然写入文件
function opt_update($ID, $post) {
$args = array(
'post_type' => 'post'
);
$opt_array = array();
$the_query = new WP_Query($args);
while ($the_query -> have_posts()): $the_query -> the_post();
array_push($opt_array, array(
'price' => get_field('price')
));
endwhile;
wp_reset_postdata();
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/blablabla/json/data.json', raw_json_encode($opt_array));
}
add_action('publish_post', 'opt_update', 10, 2);
acf/save_post 钩子就可以了