更新post meta在初始数据库插入后未更新值

时间:2018-06-07 12:10:25

标签: php wordpress

我正在编写一个函数,用户可以将帖子固定到新列表的顶部。

这是我的工作脚本

function pin_posts_options($post_id) {
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || 
    wp_is_post_revision($post_id)) { // Don't do autosave or revision
    return;
}

global $post;

$post_types = ["audio", "gallery", "post","video"];
if(in_array($post->post_type, $post_types)) { 
    echo "<div class='misc-pub-section misc-pub-section-pin-post misc-pub-section-last'> <span>Pin post?</span>"; 
    $checked = "";
    $value = "";

    if(get_post_meta($post->ID, "pin_post", true) == 1) {
        $value = 1;
        $checked = "checked";
    }

    echo "<label><input id='pin_post_control' type='checkbox' name='pin_post' value='" . $value . "' $checked /></label>"; 
    echo "</div>"; // Close dialog for pin post
    }
}

function pin_posts_options_save($post_id, $post, $update) {
   if(isset($_POST['pin_post'])) { // Check for the value
    update_post_meta($post_id, 'pin_post', 1);
   }
   else {
    update_post_meta($post_id, 'pin_post', 0);
   }
}

add_action("post_submitbox_misc_actions", "pin_posts_options");
add_action("save_post", 'pin_posts_options_save', 10 , 3); 

然而,它似乎没有起作用。当我点击“更新”时,它将为我插入后元,但一旦插入该值,它将不会从0更改 - &gt; 1或1 - &gt; 0

感觉我错过了一些明显的东西,因为我有其他类似的脚本可以正常工作。

P.S。我还有一些javascript控制,当用户用户选择固定帖子时

jQuery(document).ready(function(){
   jQuery('#pin_post_control').change(function(){
    if(!$(this).val()) {
        $(this).val("1");
    }
    else {
        $(this).val();
    }
});

});

0 个答案:

没有答案