This release introduces two modes of operation when creating a WordPress Meta Box: Array Mode and Extract Mode. The default mode is Array and will typically work well for the majority of users. Learn how and when to use Extract mode.
This release introduces two modes of operation when creating a WordPress Meta Box:
- Array Mode (
WPALCHEMY_MODE_ARRAY
): Meta box data is stored in a single meta entry as an associative array. - Extract Mode (
WPALCHEMY_MODE_EXTRACT
): First tier variables (fields) are each stored as individual meta entries.
Tip: Select 720 HD mode and watch it fullscreen or click the YouTube button to watch it on YouTube.
What Am I Talking About, and Why Should You Care
The WPALCHEMY_MODE_ARRAY
mode is the default mode and is currently how the PHP class operates. The class will store all of its values as an associative array in a single meta entry (in the wp_postmeta table). This mode will typically work well for the majority of users.
But, there are instances where you can use meta data directly in WordPress functions. Such is the case with the query_posts()
function. This function allows you to use certain values (meta_key
, meta_value
and meta_compare
) to directly access post meta data. In these cases it is useful to have access to a single meta entry (name/value pair) versus an associative array entry.
The WPALCHEMY_MODE_EXTRACT
mode will extract all the first tier variables (fields) and create individual meta entries in the wp_postmeta table.
So, How Do I Use It
Setting your meta box to use WPALCHEMY_MODE_EXTRACT
mode is straight forward, you would do something like the following:
$custom_metabox = new WPAlchemy_MetaBox(array ( 'id' => '_custom_meta', 'title' => 'My Custom Meta', 'template' => TEMPLATEPATH . '/custom/meta.php', 'mode' => WPALCHEMY_MODE_EXTRACT, 'prefix' => '_my_' ));
What is The Prefix For?
The prefix
parameter allows you to prefix all the first tier variables (fields) with a string value. This is important because when using WPALCHEMY_MODE_EXTRACT
mode, you have to take care to avoid name collisions with other meta entries. The easiest way to do this is to add a prefix to your variables.
You can do this automatically for all extracted values in the meta box (as seen above, using the prefix
parameter) or you can do this manually in the meta box template file when declaring your form fields, as such:
// manually adding "_my_" prefix to each field name <?php $mb->the_field('_my_name'); ?> <input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/> <?php $mb->the_field('_my_description'); ?> <textarea name="<?php $mb->the_name(); ?>" rows="3"><?php $mb->the_value(); ?></textarea>
Using the values in your theme template files is equally straight forward, the following are all equivalent ways of accessing the variables (fields):
$custom_metabox->the_value('name');
$custom_metabox->the_field('name'); $custom_metabox->the_value();
// uses the true meta entry name directly echo get_post_meta(get_the_ID(),'_my_name',TRUE);
// using get_the_name(), both examples work echo get_post_meta(get_the_ID(),$custom_metabox->get_the_name('name'),TRUE); echo get_post_meta(get_the_ID(),$custom_metabox->get_the_name('_wpa_name'),TRUE);
In the case of the query_posts()
function you could do something like:
query_posts('cat=3&orderby=meta_value&meta_key=' . $custom_metabox->get_the_name('name'));
Download
WPAlchemy MetaBox Class, this project is on github
I Need Your Help
I generally do a better job of adding features which I use personally. This particular feature was added because its had a few requests, and I do see a need for it, however I haven’t yet had a requirement to use it myself.
Other than testing the feature and having a general idea of how you are going to use it, I need your feedback. If you are using this release please give me your candid thoughts, opinions and wishes. This will help me shape a solid and elegant API.
Thanks for the great work.
My goal right now is to order a future event list by timestamp.
I’ve created the meta box fields for the user to put in the start date and start time. I then run the php function “strtotime” on the values from the fields and put the result in a hidden field called “serial.” I’m ordering my query based on that hidden field.
Everything works great except that the posts don’t show up on the initial publish. They only show up after hitting the update button. I’m assuming that the data from the serial field is not being committed on the initial publish. How can I make my code better so that the data is committed to the serial field right away?
My Meta Box Code:
get_the_value('event_start_date'); ?>
get_the_value('start_time_hr'); ?>
get_the_value('start_time_min'); ?>
<input type="hidden" name="the_name('event_start_serial'); ?>" value=""/>
My Query Code:
<a href="">
The code is being stripped out in the comment preview even though its surrounded by code tags – hopefully the actual comment will display correctly.
Thanks for the help and the great plugin.
I am having some type of strange issue with this in combination with the metabox script.
I am essentially trying to include events on the right hand side of every page of the site I am building by means of a PHP include.
The php code I am including in the right hand menu is as follows:
http://pastebin.com/yhJAsNyT
The issue I am having is that the values for each of the custom fields are only showing up when the php include is being called from a page were the main page contents are for this same post type.
For all other pages/post types the query post elements seems to be working however none of the custom fields value show up…
Is there something I need to change to make this work correctly or am i doing something wrong here?
As mentioned, this exact code works perfectly when the page being loaded has the main post content related to that post type… just not for the other ones.
@Binarybit, I’ve ammended the code, see http://pastebin.com/a9diLEfy
@Josh Byers, I hope everything is still working out for you with the recent code updates to wpalchemy.
I am trying to WP Query custom posts to use inside of a dropdown/select box using WP Alchemy, does anyone have any tips?
I would like for the options to display the postname, but their value be the Post ID.
Any clues?
Hi Dimas,
i stumbled upon your class some time ago and was instantly impressed. Following your repo on Github now. Thanks!
Hey Dimas, the option WPALCHEMY_MODE_EXTRACT doesn’t affect fields created by the functions have_fields() or have_fields_and_multi(). The field values are stored as an array. Is there a way to store them as individual meta entries as well? Same meta_key different meta_id? Thank you!
It took me a few hours to figure out how to run WP_Query based on custom fields created with WPAlchemy in WordPress 3.1 – the parameters meta_key and meta_value have been deprecated: Function_Reference/WP_Query#Custom_Field_Parameters
My solution was to use ‘mode’ => WPALCHEMY_MODE_EXTRACT in functions.php and call WP_Query in my template as follows:
Then run the loop with whatever fields you want to display:
This class is awesome, I’m just wondering if the values stored using this class through the differents metaboxes created can be exported/imported since I already tried it and in the new installation they’ll always start blank, so I’m just wondering if I’m doing something wrong?
@Sin, I’ve run into issues before with wordpress xml export/import, but I believe I’ve fixed this problem. If you are indeed experiencing issues, then I will investigate some more. Also, if any changes are made to the serialized exported data (search and replace), this may cause issues with PHP reading in the serialized data back in.
Hi Dimas!
Just some feedback: It might be worth considering extending the prefix definitions to also apply to the various javascript vars and related css classes that control metabox duplication, manipulation, and media insertion. I just ran into a case where such naming conflicts (particularly with docopy, dodelete, tocopy) caused erratic behavior since apparently another dev has also used WPAlchemy w/ MediaAccess.php on the same site prior to me. I was able overcome this with brute-force renaming in this case, but having the ability to set the prefix once to easily avoid conflicts would probably be a benefit to others.
Thanks for all your hard work! It is truly appreciated.
Dameian, thanks for your feedback, Interesting scenario and one that has crossed my mind before. I will take your suggestion under consideration.
If wpalchemy was installed, lets say in /wp-content/wpalchemy to allow access to other devs, so everyone can include the same file would this have solved your scenario?
How can recreate your scenario to better understand the behavior?
Do me a favor if you will, can you file this as an issue on github?
Hi Dimas,
Sorry for the delay in responding. I forgot to check the box to be notified when replies were made.
My scenario:
Another dev set up a custom post type then apparently used WPA to allow the client to add paypal buttons and such to posts/pages.
I then created a custom slideshow builder using WPA for her that are also used on pots/pages. I use the repeating fields feature. When I went to add another group, it would create two groups. Then I looked at the code and saw why: two versions of WPA running causing any WPA events to fire twice.
In *most* cases, yes, a public library/bin for WPA would probably help this scenario. Having WPAlchemy installed in a public bin/library wouldn’t have mattered in my particular case though. I use a modified version of the media access class. This hackified version I use incorporates WP AJAX callback for an improved UX. It also grabs the selected media attachment’s ID as opposed to the file url, allows me to do all kinds of fun stuff with the media such as grab various thumbnail/image sizes, use exif data, grab alt and title text, etc.
That’s just my case and I am admittedly a code tinkerer. Having WPA in a public lib/bin may very well help others that do not need the features I added.
Also worth considering is that a dev might have deployed on an older version of WPA in their code. Upgrades to the WPA public library could potentially break what they’ve done if overwritten by a newer version. I realize this scenario is more far-fetched, but certainly possible.
From my (admittedly limited) perspective, allowing the prefix set when configuring the metabox to also trickle down to the css, php, and js vars in sort of a pseudo-namespacing setup may offer more flexibility while still eliminating conflicts.
I hope this helps. I will go file a github issue now.
Thanks again for all your work and for your consideration!
is it possible to use array mode for all metaboxes and extract mode for just 1 metabox value for better optimization ( like i just need it for ratings)
also if i migrate from Array mode to Extract mode.
is there any way so that old meta data is not lost.
basically i want my theme to be compatible with its earlier versions.
thanks a lot for awesome API.
@atinder, yes you can keep it in regular mode and just use the “save_filter” to write an additional value using the regular wordpress “update_post_meta”.
switching modes should switch how the data is saved transparently, if you find that its not please let me know.
thanks for the input dimas it was really helpful.
keep up the good work.
Hi,
this storage mode is just what I needed, but I’ve ran into an issue. I’m using a custom build front-end submission form to create posts with various meta fields. The front-end meta values are saved properly and have matching slugs with the ones created from the back-end (with WPAlchemy).
But due to some reason WPAlchemy doesn’t recognize the values created from the front-end. Using the_value(‘filed_name’); ?> prints a blank value. However ID, ‘field_name’, true); ?> prints the proper value.
Hi Ivaylo: for that last comment, is that just a typo in the comment, or is it an actual typo in the code: the_value(‘filed_name’); ?>
(where field_name is misspelled) That could be your problem…
I’m having a similar issue as Ivaylo. I’m inserting data away from WPAlchemy using the standard WordPress update_post_meta() function. I can verify that the custom field attached to that name is in fact pulling in by cross referencing the Custom Fields section of WordPress. This is the same variable I am using through WPAlchemy. However, WPAlchemy does not pick up that value using the_value().
To further verify, I can input data into the WPAlchemy box and the meta value in question is in fact overwritten by WPAlchemy, verifying that I am using the correct meta key and meta value pair.
I am using the WPALCHEMY_MODE_EXTRACT.
Added in-depth video about WPAlchemy Storage Modes, see above.
I’m having what I believe is a similar issue to Brian and Ivaylo both, though a different application. I’m trying to get the select state in WP Alchemy to respect the value stored in the custom field via add_post_meta. Here’s a link to my comment on the main article: http://www.farinspace.com/wpalchemy-metabox/#comment-17917
Hi there,
Brilliant class, thank you!
I am having some issues querying meta_keys once I changed the mode to: WPALCHEMY_MODE_EXTRACT. I want to get the three most recent posts in a category, where the start date is before todays date and the end date is after todays date.
Please see my query below, thank you so much for your help
Hoping this may be of help to another developer using this great plugin! 🙂
This may not be possible, but I’ll ask anyway.
I am creating a automatically, within a plugin, the updating it based on a json response.
Essentially, the post is created, the json request sent, then based on the response I need to add meta data to the post. This all works, in the DB I see
_mypostype_meta_fields
_myprefix_fieldone
_myprefix_fieldtwo
etc.
I am using extract mode.
However since the post was never published or update through WP, wpalchemy does not know the _myprefix_ fields exist, there is nothing about them in the array and thus no values in the fields when the post is edited via admin.
Is there another way to do what I want to do, or can’t it be done?
Geez, I previewed that and thought it made sense, but it really doesn’t. 🙁
The second sentence should read:
I am creating a post automatically, within a plugin, then updating it based on a json response.
Hi Dimas!
I need to switch existing data from array to extract mode, what should i do? I understand I need to use the save_filter, but i’m struggling a bit.
Is a metadata in extract mode the same as a custom field?
thanks
I believe I have everything set up correctly and I am using the WPALCHEMY_EXTRACT_MODE on my custom meta box.
I have a checkbox set up that when checked should let the user add that post to the front end of the site. When I check the box it saves, but nothing is coming through on the front end. My code is as follows:
http://pastebin.com/eMkaR5K9
Any thoughts?
How would I do a query for posts with a meta field that have a certain checkbox checked?
I have my metabox setup to use EXTRACT and the box values are post ids – so basically, I am looking for the posts that have a certain id checked.
I tried to use this in my query arguments:
‘meta_query’ => array(
array(
‘key’ => ‘_workshop_speakers’, //checkbox array
‘value’ => $post->ID, //id to query for
‘compare’ => ‘IN’
)
}
but it’s not finding anything.
First let me say thank you for this class! It’s amazing.
I have a very similar issue to Federico Vezzoli and I did not see a response to his query. I have a number of items stored in WPALCHEMY_MODE_ARRAY and now I find I need to create a search routine that tests against meta data. I will have to convert a couple hundred entries with multiple fields each to WPALCHEMY_MODE_EXTRACT, and am wondering if there is a ready built automation for this that I simply don’t see in the docs.
Thanks again!
I did this. Seems to work. I’m sure it could be improved upon.
http://pastebin.com/J44LQBv4
I have this in a plugin and working no problem. Now I want to get the meta data in a theme file. The theme file is in a child theme. I have spent all day working on this with no joy. How would I do this? Do I need to include anything?
Have a look at Using The Meta Box Values In Your Template