I have a real estate site. On property pages users can save homes as a favorite. The property gets added to the user meta data. I can successfully add and delete entries from the single property page with no issues.
I have "view favorites" page that displays all saved homes. I'd like users to be able to remove saved homes from this page. My issues is the homes are being displayed in a foreach and if you choose to remove one home...all homes end up being removed. I'm sure my issue is that the foreach is looping the delete_user_meta function but I can't figure out how to set it up correctly.
Each property has an array of entries (bed / bath / price / etc). I'm assuming I need to POST the array and use the posted array in the delete function but I can't figure it out.
My code:
<?php
$user_id = get_current_user_id(); //Get current user ID
global $current_user; wp_get_current_user();
$user_id = $current_user->ID;
$meta = get_user_meta($user_id, 'savedhome');
?>
<div class="row"> <!--defines and displays each result successfully-->
<?php foreach ($meta as $key => $value) { ?>
<?php
foreach($value as $k1 => $v1) {
if ($k1 == 'Beds'){
$k1beds = $v1;
}// end if beds
if ($k1 == 'Baths'){
$k1bath = $v1;
}// end if baths
if ($k1 == 'Price'){
$k1price = $v1;
}// end if Price
}//end foreach value as k1 v1
?>
<div class="favprice">
<?php echo "$$k1price";?>
</div>
<div class="favbed">
<?php echo "$k1beds";?>
</div>
<div class="favbath">
<?php echo "$k1bath";?>
</div>
</div><!-- close row -->
<?php
echo '<pre>' , var_dump($value) , '</pre>'; //displays correct array for each home
if(isset($_POST["Remove"])){
delete_user_meta($user_id, 'pluginlink', $value);
}//end if
//Below will determine if home exists in user meta and display the correct save/delete button
$meta = get_user_meta($user_id, 'savedhome');
if(array_search($k1price, array_column($meta, 'Price')) !== false) {
$savebutton = "display: none;";
$deletebutton = "display: block;";
}
else {
$savebutton = "display: block;";
$deletebutton = "display: none;";
}
?>
<div class="buttondisplay" style=" <?php echo $savebutton; ?> ">
<form method="post" enctype="multipart/form-data">
<button type="submit" name="submit" style="background-color: #fff; padding-left:3px;" ><i class="fa fa-heart-o" aria-hidden="true" ></i></button>
</form>
</div><!-- end save button -->
<div class="deletebutton" style=" <?php echo $deletebutton; ?> ">
<form method="post" enctype="multipart/form-data" >
<button type="submit" name="Remove" value="<?php echo $k1mlsid; ?>" style="background-color: #fff; padding-left:3px;"><i class="fa fa-heart" aria-hidden="true" ><?php echo $loginouttext; ?></i></button>
</form>
</div><!-- end delete button -->
<?php } //end foreach meta as key value
?>