31 responses to “WPAlchemy MetaBox: Data Storage Modes”

  1. Josh Byers

    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.

  2. Binarybit

    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:

    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.

  3. Joe

    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?

  4. Kaiser

    Hi Dimas,

    i stumbled upon your class some time ago and was instantly impressed. Following your repo on Github now. Thanks!

  5. Christoph

    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!

  6. Lisa

    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:

    // usually needed to get data from custom fields created by WPAlchemy
    global $custom_metabox;
    // set up arguments for WP_Query
    $args = array(
    'post_type' => 'page',
    // must use meta_query as an array with WP 3.1+ (instead of meta_key or meta_value)
    'meta_query' => array(
    // only works if 'mode' => WPALCHEMY_MODE_EXTRACT is used for $custom_metabox in functions.php
    'key' => $custom_metabox->get_the_name('name'),
    $my_query = new WP_Query( $args );

    Then run the loop with whatever fields you want to display:

    while ($my_query->have_posts()) : $my_query->the_post();
  7. Sin

    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?

  8. Dameian

    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.

  9. Dameian

    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!

  10. atinder

    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)

  11. atinder

    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.

  12. atinder

    thanks for the input dimas it was really helpful.
    keep up the good work.

  13. Ivaylo


    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.

  14. Wendy

    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…

  15. Brian

    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.

  16. Jonathan Wold

    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

  17. CC

    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! :)

    $today = time();
    //set up the query for the three most recent shows
    $args = array(
    'post_type' =&gt; array('show-listing'),
    'orderby' =&gt; 'meta_value_number',
    'meta_key' =&gt; '_my_start_timestamp',
    'showposts' =&gt; 3,
    'order' =&gt; 'ASC',
    'meta_query' =&gt; array(
    'key' =&gt; '_my_start_timestamp',
    'value' =&gt; time(),
    'compare' =&gt; ' 'NUMERIC,'
    'key' =&gt; '_my_end_timestamp',
    'value' =&gt; time(),
    'compare' =&gt; '&gt;=',
    'type' =&gt; 'NUMERIC,'
    // The Query
    $featured_shows = new WP_Query($args);
    // Display the posts
    while ( $featured_shows-&gt;have_posts() ) : $featured_shows-&gt;the_post();
    echo '';
    echo '';
  18. Katherine

    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

    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?

  19. Katherine

    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.

  20. Federico Vezzoli

    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?


  21. Dan Gavin

    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:


    Any thoughts?

  22. Jesse

    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(
    ‘key’ => ‘_workshop_speakers’, //checkbox array
    ‘value’ => $post->ID, //id to query for
    ‘compare’ => ‘IN’

    but it’s not finding anything.

  23. Catherine Sandrick

    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!

  24. Catherine Sandrick

    I did this. Seems to work. I’m sure it could be improved upon.


  25. Joseph

    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?

Leave a Reply