Wordpress: Get all values of a custom field - php

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.

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)

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 value in my wp_postmeta table like a:1:{i:0;s:1:"5";}.....I need to get the last value.that is the value inside the " ".in wordpress

I am working on Wordpress. I use custom fields and one of the field category contains field type as taxonomy.
While inserting,the postmeta table contains this JSON value: a:1:{i:0;s:1:"5";}.
I need to get the value inside the " ".That is 5.
Simply use native get_post_meta function which unserializes data itself:
echo get_post_meta(POST_ID_HERE,'post_meta_key_name_here',true)[0];
or this
$value=get_post_meta(POST_ID_HERE,'post_meta_key_name_here',true);
echo $value[0];

Conditional PHP unserialize function

First of all, I am sorry if this appears to be a silly question. I am new with this "unserialize" command and I have been reading much information here about it, but I still really don't know how to implement it to get what I need.
Let me explain what do I have now, and what do I need.
I have this code now:
$consultametacat = $wpdb->get_row("
SELECT post_id,
meta_key,
meta_value
FROM cg_postmeta
WHERE post_id = '".$consultapost->ID."'
AND meta_key LIKE '_category_permalink'
",
ARRAY_A
);
This saves the results of that query to the database in ARRAY_A
Then I get that value on other lines like these:
$category_color = get_tax_meta($consultametacat['meta_value'],'pm_color_field_id');
$category_name = get_term_by('id', $consultametacat['meta_value'], 'category',ARRAY_A);
$category_name = get_term_by('id', $consultametacat['meta_value'], 'category',ARRAY_A);
$category_link=get_category_link($consultametacat['meta_value']);
It worked good so far, as the meta_value was an integer number until two weeks ago. Now, old entries in "_category_permalink" field in the database has integer numbers, but new entries have serialized data like this:
a:1:{s:8:"category";s:2:"23";}
So, what I need to do is to put a condition and unserialize according to that condition, to always get an integer number. The logic will be like this:
-if the results for
$consultametacat = $wpdb->get_row("
SELECT post_id,
meta_key,
meta_value
FROM cg_postmeta
WHERE post_id = '".$consultapost->ID."'
AND meta_key
LIKE '_category_permalink'
",
ARRAY_A
);
is an integer number, then save that value normally. But if it results in a serialized line, then unserialize, get the numeric value (in my example, the 23 from the line a:1:{s:8:"category";s:2:"23";}) and save that numeric value.
How to achieve this?
Thank you very much for all your help in advance.
You have to detect first that your result is serialized and then take care of that, if not nothing more is to do:
if(strpos($consultametacat,':{')!==false){
$consultametacat = unserialize($consultametacat);
$consultametacat = array_shift($consultametacat);//or = $consultametacat['category'];
}
strpos() detects :{ in $consultametacat and returns his position and every serialized data has this part in it. But serialize() can also serialize objects and other types, so research for yourself a little more on that topic.

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
}

WordPress: PHP: How to perform a nested sort within a category for the desired results

There is a plugin that I am trying to adapt for my meta_tags for use with WordPress.
It is called Categories by Title by http://www.mikesmullin.com.
What I am trying to achieve is a nested sort. I have three meta_keys. A selection-no, release-month, and release-year. I would like to sort posts within their category by release-year (asc), release-month (asc), then selection-no (asc). For example: 1955 10 selection-no-1, 1956 10 selection-no-2, 1956 10 selection-no-5, and so on.
I have modified the code, however, it only sorts by the last meta_key listed, by release-year.
Here is the code.
add_action('pre_get_posts','sort_categories_by_title');
function sort_categories_by_title($x) {
if(is_category()) {
$x->query_vars['orderby'] = 'meta_value';
$x->query_vars['meta_key'] = 'selection-no';
$x->query_vars['meta_key'] = 'release-month';
$x->query_vars['meta_key'] = 'release-year';
$x->query_vars['order'] = 'asc';
}
}
Any help would be greatly appreciated. Thank you.
Have a look at usort. You supply a comparison function which can look inside each item and choose what to order by.
In your example, every time you do $x->query_vars['meta_key'] you overwrite the previous value, so you're only adding the last meta_key as a selection option.
On top of that WordPress only supports pulling by a single meta_key/meta_value comparison. To get the more complex comparison that you're after you should be filtering on posts_where_paged to add in more comparisons and posts_orderby to construct the order by clause that you need. You'll need a basic knowledge of raw SQL query writing to accomplish this.
You might consider restructuring your data as well and putting the release-date in to a real date format and using typecasting to take the string format of the meta_value and cast it as a date field to then do proper date based sorting operations on it.

Categories