首页 > 解决方案 > 如何从用户元中删除帖子/产品?

问题描述

在我的项目中有一个部分:最喜欢的。在帖子上,当您单击此按钮时,有一个按钮可以添加到收藏夹,帖子 ID 放在当前用户元数据上。这是按钮代码:

<span data-post-id="<?php the_ID(); ?>" data-user-id="<?php echo $current_user->ID; ?>" class="receipt_favorites"></span>

这是ajax的Jquery:

jQuery('.receipt_favorites').on('click',function(e){
    var postID = jQuery(this).attr("data-post-id");
    var userID = jQuery(this).attr("data-user-id");

    data = {
            'action': 'dicaprio_add_fav_recipe',
            'postID': postID,
            'userID': userID
        }
        jQuery.ajax({
            type:'post',
            url: object_url.url,
            data: data,
            cache: false,
            success: function(data) {
            }
        });
    e.preventDefault();
 });

这是php:

add_action( 'wp_ajax_nopriv_dicaprio_add_fav_recipe', 'dicaprio_add_fav_recipe' );
add_action( 'wp_ajax_dicaprio_add_fav_recipe', 'dicaprio_add_fav_recipe' );

function dicaprio_add_fav_recipe() {

$postID = $_POST['postID'];
$userID = $_POST['userID'];

$current_fav_list = get_user_meta( $userID, 'favorite_list_recept', true );

if ( is_array( $current_fav_list ) ) {
    if ( in_array( $postID, $current_fav_list ) ) {

    } else {
        $current_fav_list[] = $postID;
    }
} else {
    $current_fav_list   = array();
    $current_fav_list[] = $postID;
}
update_usermeta( $userID, 'favorite_list_recept', $current_fav_list );
}

这三位一体做得很好,帖子被添加到收藏夹。但是如何从收藏夹中删除它?这是我的问题。我知道为此我需要创建一个按钮,例如:

<span data-post-id="<?php the_ID(); ?>" data-user-id="<?php echo $current_user->ID; ?>" class="remove__favore"></span>

并为 ajax 使用相同的 Jquery,仅通过此类激活:remove__favor。但是php函数部分呢?我认为这会更容易,但我无法编写删除最喜欢的帖子的函数。你能告诉我如何重新安排移除吗?我知道对于专业人士来说这是一个菜鸟问题,但是我花了很多时间却无法解决。

标签: phpwordpress

解决方案


推荐阅读