Remove meta from user - php

I have added ACF Post Object field to a user(field key is field_5beda4b10dc7d). I am trying to remove this field value from a user based on the user ID, ACF Key, and post id. Am I missing something?
delete_user_meta(1, 'field_5beda4b10dc7d', 128);

You are using delete_user_meta() wrong. Look at the documentation:
https://codex.wordpress.org/Function_Reference/delete_user_meta
The third parameter does the following:
$meta_value (string) (optional) Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete metadata entries
with this value. Otherwise, delete all entries with the specified
meta_key. Pass null, false, or an empty string to skip this check.
(For backward compatibility, it is not possible to pass an empty
string to delete those entries with an empty string for a value.
What you currently say is: Delete user meta where
user_id = 1
meta_key = field_5beda4b10dc7d
meta_value = 128
This is probably not true in most cases. Probably you just have to ommit the third parameter in order to achieve your goal.

Related

Matching array of string to numbers to get database values

In my database, I have a status column where I'm using numbers to represent a status of a product. Each of these numbers represents a string value, for example 1 is open, 2 is closed, etc. Now to display a count of these statuses in my webpage, I am converting the following numbers into a string to display them to the user:
array(1=>'Open',8=>'Hot',2=>'Closed',3=>'Transacted',4=>'Dead',9=>'Follow Up',11=>'Working')
Now I have this count as a clickable link, where the user can click the count and it takes them to a new page showing the details of that item. For which I'm using this:
<a target='_blank' href='".site_url('reports/viewall?status=' . $status)."'>".$num."</a>
This correctly passes the argument to the URL and shows http://localhost/reports/viewall?status=Closed
Now I'm storing this variable in my controller as $status, but I cannot use this string value in my model query which is this since it is giving the string value of status and not the number related to it:
SELECT * from TABLE1 where status = $status
Controller Class where I'm storing the $status:
$status = $this->input->get('status');
$content['individualleads'] = $this->leads_model->get_all_leads($status )->result();
I think the best approach would be to pass the integer in the URL and use the array text value only when it is text meant for the user. So just:
http://localhost/reports/viewall?status=2
Another alternative is to flip the array and access the text key:
$status_num = array_flip($array)[$status];
Or search the original array:
$status_num = array_search($status, $array);
Probably more standard might be to have a status_types table with the integer status_id and text status_text for each status and join this when retrieving the status.

How to get main meta_key's inner key? Inner key is on json format and need to get result in WordPress WP Query (With pagination)

How to get main meta_key's inner key? Inner key is on json format and need to get result in WordPress WP Query (With pagination)
Need solution on main WP_Query argument. Not in inner condition.
I have custom post type and meta stored in one meta key name is ="lp_listingpro_options" and on that meta key added multiple json data.
"Meta stored in databse on 'lp_listingpro_options' key is:"
I need to get all Plan_id is = 195
Can you please help me on that critical issue
a:35:{s:12:"tagline_text";s:15:"Atmen ist Leben";s:7:"gallery";s:0:"";s:12:"price_status";s:6:"notsay";s:10:"list_price";s:0:"";s:13:"list_price_to";s:0:"";**s:7:"Plan_id";s:3:"195"**;s:16:"lp_purchase_days";s:0:"";s:11:"reviews_ids";s:0:"";s:15:"claimed_section";s:7:"claimed";s:26:"listings_ads_purchase_date";s:0:"";s:30:"listings_ads_purchase_packages";s:0:"";s:4:"faqs";a:2:{s:3:"faq";a:1:{i:1;s:0:"";}s:6:"faqans";a:1:{i:1;s:0:"";}}s:14:"business_hours";a:5:{s:6:"Montag";a:2:{s:4:"open";s:5:"08:00";s:5:"close";s:5:"17:00";}s:8:"Dienstag";a:2:{s:4:"open";s:5:"08:00";s:5:"close";s:5:"17:00";}s:8:"Mittwoch";a:2:{s:4:"open";s:5:"08:00";s:5:"close";s:5:"17:00";}s:10:"Donnerstag";a:2:{s:4:"open";s:5:"08:00";s:5:"close";s:5:"17:00";}s:7:"Freitag";a:2:{s:4:"open";s:5:"08:00";s:5:"close";s:5:"17:00";}}s:21:"lp_medical_report_pdf";s:0:"";}
Look at the database meta_key stored image is
https://www.awesomescreenshot.com/image/15262224?key=c4c4290515f14fedc7ff39995cc682c7
That meta_value payload is not JSON. Rather, it uses php's serialization format for its objects and arrays. To look for a particular item in the meta_value, you load it into a php object and examine that object.
You can load it into a php value with deserialize(). But within WordPress you don't need to worry about that. If you use WordPress's get_post_meta() function it will return the item already deserialized.
Then you examine the array to find what you want.
$listingproOptions = get_post_meta( $postId, 'lp_listingpro_options', true );
print_r( $listingProOptions ); /* remove this line after testing */
if ( isset ($listingproOptions['Plan_id']) ) {
$planId = $listingproOptions['Plan_id'];
}

What is the difference between using true or false inside update_user_meta()

I am trying to update a meta value inside my database using update_user_meta. If I pass true into the function it doesn't update the meta value, but if I use false it does update. So whats the difference between the two?
update_user_meta(1, 'event_year', '8', true);
update_user_meta(1, 'event_year', '8', false);
The description for last parameter is not the right method you are following.
it will be like,
//changes 'true' to '8'
update_user_meta(1, 'event_year', '8', true);
the 3rd parameter will replace with 4th parameter, so in your case it will replace true with 8
This function has 4 parameters:
$user_id : The user id.
$meta_key : Metadata name to be updated.
$meta_value : Metadata new value.
$prev_value :previous value of metadata. It is optional so if supplied, only metadata with that value will be updated otherwise all metadata with supplied $meta_key will be updated.
Okay, referring to https://codex.wordpress.org/Function_Reference/update_user_meta we can say that fourth parameter defines
> Previous __value__ to check before removing.
See, it is THE VALUE that is checked. So, before updating your event_year field wp checks if this field stores this defined value. And you say - the stored value is true. I'm not really sure how true is converted in query text, but probably after passing it to query text wp cannot confirm that removed value is same as true. That's why update does not happen.
In case of false (and false can be considered same as default value for fourth parameter - '' (empty string)) you tell wp - nothing to check here, just update if value has changed.
$prev_value => Optional. If specified, only update existing metadata entries with the specified value. Otherwise, update all entries.
update_user_meta() relays to update_metadata() and this explanation is there https://core.trac.wordpress.org/browser/tags/5.2.1/src/wp-includes/meta.php LINE 151

Value not compatible when doing merge in DB2

I'm trying to perform a merge based on a parameter from a previous select within a php script but I"m getting the error "SQL0408 - Value for column,variable, or paramter QUANTITY not compatible"
In my destination table QUANTITY is data type INTEGER
In my select query, I'm casting the value as an int (which it already is in the table, I'm just casting everything to be safe)
cast(MAX(orqtyc) as int) AS QUANTITY,
Then in my MERGE I'm casting as INT
MERGE INTO HNORMANTEST.PLACEMENTS AS P
USING(VALUES(
CAST(:QUANTITY as INT),
))
Using this param
$params = [
":QUANTITY" => $row["QUANTITY"],
];
Why would it say it's not compatible?
Did you try it by putting directly value instead of via param and see if it works or not.
Another thing you can try is remove the first casting which may not be needed. As you said QUANTITY is already a INT datatype.
Please try both the variation. If both variation givens same error then there might be some product limitation/bug.
You need to pass on the DB2 version where you have tried to look further in it.

Wordpress - how to add a post meta keeping the value unique?

I need to add a post meta keeping and I want the value to be unique, but according to the add_post_meta() codex there is only the option to set the key as unique. Can I do the same with the value?
The only solution I found is to check the post meta doing a get_post_meta(), then search into the array for a duplicate value, but maybe it's too intricate.
Check it manually over an Database query like:
$search = "What you searching for?";
if($wpdb->get_var($wpdb->prepare("SELECT COUNT(`meta_value`) FROM $wpdb->postmeta WHERE `meta_value`=%s", $search)) == 0) {
// Dont exists,.. Add a new post meta for example
}

Categories