【问题标题】:How to delete only user meta value (not key) from usermeta table in wordpress?如何从wordpress中的usermeta表中只删除用户元值(不是键)?
【发布时间】:2015-05-26 06:36:24
【问题描述】:

我将多个图像存储在媒体库中,并将其 id 保存在 wp_usermeta 表中。我的元键是“图像”和元值,例如“279,280,281”。现在我从这样的表中获取图像:

<?php $images = get_the_author_meta( 'images', $user->ID );
echo $images; //output is '279,280,281'.
 $images = explode(',',$images);
 foreach($images as $img) {
?>
      <img src="<?=wp_get_attachment_url( $img );?>" width="100" height="100" />
      <a href="<?php  echo get_edit_user_link( $user->ID ); ?>&image_id=<?= $img;?    >">Delete</a>
  <?php } ?>

现在,我不会从 wp_usermeta 表中删除特定的 id。所以,请帮我解决它。我尝试使用这种类型的代码进行删除:

 if(isset($_REQUEST['image_id'])){
    $image_id = $_REQUEST['image_id'];
// delete_usermeta( $user->ID, $meta_value = $img );  
    if (($key = array_search($image_id, $images)) !== false) {
        unset($images[$key]);
    }   
    wp_delete_attachment($image_id);
    }

【问题讨论】:

    标签: php wordpress unset


    【解决方案1】:

    您可以使用 get_user_meta 来获取所有图像 ID,然后删除您想要的并更新它。像这样的

    $ID_TO_REMOVE = 279;    
    $images = get_user_meta( $user_id, 'images', TRUE ); 
        $images = explode(',',$images);
        foreach($images as $image){
           if((int)$image != $ID_TO_REMOVE){
             $new_images .= $image.','; 
           }
        }
    
        update_user_meta($user_id, 'images', $new_images);
    

    我没有测试过这段代码,但应该是这样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-27
      • 2014-03-27
      • 1970-01-01
      • 2018-02-05
      • 2015-07-08
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      相关资源
      最近更新 更多