如何在wordpress中通过post ID更新(保存)变量


How to update (save) a variable by post ID in wordpress?

我正在尝试更新一个由会话接收的变量。现在它将保存数据,但对于每个帖子都是一样的。它需要通过post id保存值。

代码的一部分是(它是高级自定义字段模板的一部分):

<?php
function create_field( $field ) 
{
$value = $_SESSION['updatevalueMax'];
echo '<div id="value">' . $value . '</div>';
}
    function update_value( $value, $post_id, $field )
    {
        return $value;
    }
?>

更新帖子时,update_value函数将被激活。如何使$value由$post_id更新?谢谢

您需要使用Wordpress:注册过滤器

function my_plugin_update_value( $value, $post_id, $field )
{
    // Do something and update $value.
    return $value;
}
add_filter('acf/update_value', 'my_plugin_update_value', 10, 3);

尽管在您的示例中$value只是发送到函数中的值。我不确定你想用什么来更新它。

您需要在函数中编写一个过滤器。php

按照网址来更好地理解。

http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data