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
}
Related
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'];
}
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.
I would like to know how I can update a value stored in an array, in crate.io
I have a blog table - blog_tbl
A column, with data type array - tags
A id column
Inside the tags column I have - ["tag1","tag2","tag3"]
I would to know how I would go about changing 'tag1' to 'tag99'
I tried
update blog_tbl set tags['tag1'] = 'tag99' where id = '1';
Also how would I add one the the end? so making it -
["tag1","tag2","tag3","tag4"]
many thanks
Unfortunately it's not possible currently. Array elements can only be selected using the subscript notation (e.g. select tags[1] from blog_tbl;) but not updated. Maybe add a GH issue requesting that feature.
You can use the pattern found here: https://crate.io/docs/reference/sql/occ.html#optimistic-update
However, that requires you to perform the modification on client side. Pseudo code:
updated = False
while not updated:
cursor.execute('SELECT array_field, "_version" FROM table WHERE id=1')
row = cursor.fetchone()
current_array_field = row[array_field]
current_array_field.append('newtag')
cursor.execute('UPDATE array_field = current_array_field WHERE id=1 AND "_version" = row[version]')
if cursor.rowcount > 0:
updated = True
This will make your update semi safe for concurrent updates of the same field.
I've an meta field named "date"(custom field), now I'm trying to get all the posts that hasn't passed today's date, until now I did it with
if( strtotime(date("d.m.Y",strtotime(get_field("date", $post->id)))) < strtotime(date("d.m.Y")) ) { }
but it affected the pagination 2, so I need to change the get_posts() function to return the relevant results, but I'm not sure how to do it the doc talks about fetching the values with a match meta field value(which isn't exactly the case)
I'm using Verve Meta Boxes. I want to make a menu out of one of the custom fields. How can I return all of the custom field values? For example, if I had a custom select field called "fruit" and as options I have "apples", "oranges", and "bananas", how could I get a complete list of those values, as an array perhaps? I can get the ones associated with a post:
get_post_custom_values('fruit')
…but I can't work out how to get the whole list.
Thank you in advance!
In case someone still wondering:
global $wpdb;
$results = $wpdb->get_results( 'SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key LIKE "FIELD_NAME"', OBJECT );
Just make sure your postmeta table is "wp_postmeta" (default) and change FIELD_NAME with the name you created for the field in the admin.
You could do it the regular wordpress way by using the get_post_meta function in your loop.
Try this one out:
$fruits = trim(get_post_meta($post->ID,'fruits',true));
$fruits_array = explode(',',$fruits);
foreach($fruits_array as $f){
echo $f.'<br/>';
}
Basically you need to separate your fruits name with comma in your custom field so that you will be able to explode them into array and echo the values one by one.
Thanks,Dave
I wasn't able to find an elegant solution. What I ended up doing was loop through all of the posts and kept a record of unique values as I can across them, creating an array. Then I used that array to make my navigation.