I have created a custom post type called Events with the name of events

function add_events_post_type() {
    register_post_type("events", array(
        "label"             => __("Events"),
        "singular_label"    => __("events"),
        "public"        => true,
        "capability_type"       => "post",
        "hierarchical"      => false,
        "rewrite"       => array("slug" => "event"),
        "query_var"     => false,
        "show_ui"       => true,
        "supports"      => array("title", "editor", "author")
add_action('init', 'add_events_post_type');

I use the following to create my custom meta box

define('MY_THEME_PATH','/' . substr(MY_THEME_FOLDER,stripos(MY_THEME_FOLDER,'wp-content')));


function my_meta_init()
    // review the function reference for parameter details
    // http://codex.wordpress.org/Function_Reference/wp_enqueue_script
    // http://codex.wordpress.org/Function_Reference/wp_enqueue_style

    //wp_enqueue_script('my_meta_js', MY_THEME_PATH . '/custom/meta.js', array('jquery'));
    wp_enqueue_style('my_meta_css', MY_THEME_PATH . '/custom/meta.css');

    // review the function reference for parameter details
    // http://codex.wordpress.org/Function_Reference/add_meta_box

    // add a meta box for each of the wordpress page types: posts and pages
    foreach (array('events') as $type)
        add_meta_box('my_all_meta', 'Event Info', 'my_meta_setup', $type, 'normal', 'high');

    // add a callback function to save any data a user enters in

function my_meta_setup()
    global $post;

    // using an underscore, prevents the meta variable
    // from showing up in the custom fields section
    $meta = get_post_meta($post->ID,'_my_meta',TRUE);

    // instead of writing HTML here, lets do an include
    include(MY_THEME_FOLDER . '/custom/events.php');

    // create a custom nonce for submit verification later
    echo '<input type="hidden" name="my_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';

function my_meta_save($post_id)
    // authentication checks

    // make sure data came from our meta box
    if (!wp_verify_nonce($_POST['my_meta_noncename'],__FILE__)) return $post_id;

    // check user permissions
    if ($_POST['post_type'] == 'page')
        if (!current_user_can('edit_page', $post_id)) return $post_id;
        if (!current_user_can('edit_post', $post_id)) return $post_id;

    // authentication passed, save data

    // var types
    // single: _my_meta[var]
    // array: _my_meta[var][]
    // grouped array: _my_meta[var_group][0][var_1], _my_meta[var_group][0][var_2]

    $current_data = get_post_meta($post_id, '_my_meta', TRUE);  

    $new_data = $_POST['_my_meta'];


    if ($current_data)
        if (is_null($new_data)) delete_post_meta($post_id,'_my_meta');
        else update_post_meta($post_id,'_my_meta',$new_data);
    elseif (!is_null($new_data))

    return $post_id;

function my_meta_clean(&$arr)
    if (is_array($arr))
        foreach ($arr as $i => $v)
            if (is_array($arr[$i]))

                if (!count($arr[$i]))
                if (trim($arr[$i]) == '')

        if (!count($arr))
            $arr = NULL;

and my custom box looks like below

<div class="my_meta_control">
<p>Fill out this information so that events will post show.</p>
<label>Event Start Date</label>
<p><input type="text" name="_my_meta[eventstartdate]" class="date-pick" value="<?php if(!empty($meta['eventstartdate'])) echo $meta['eventstartdate']; ?>"/></p>
<label>Event End Date</label>
<p><input type="text" name="_my_meta[eventenddate]" class="date-pick" value="<?php if(!empty($meta['eventenddate'])) echo $meta['eventenddate']; ?>"/></p>
<label>Publish Date</label>
<p><input type="text" name="_my_meta[publish]" class="date-pick" value="<?php if(!empty($meta['publish'])) echo $meta['publish']; ?>"/></p>
<label>Hide Date</label>
<p><input type="text" name="_my_meta[hide]" class="date-pick" value="<?php if(!empty($meta['hide'])) echo $meta['hide']; ?>"/></p>
<p><input type="checkbox" name="_my_meta[sticky]" value="sticky" <?php if($meta['sticky'] == 'sticky') { echo 'checked="checked"'; } ?>/></p>
<div id="clearer"></div>

I am querying the posts in the custom post type on the homepage of the site using this query.

    $args = array( 'post_type' => 'events', 'posts_per_page' => 3 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        $data = get_post_custom(get_the_ID());
        $data2 = $data['_my_meta'];
        $data3 = unserialize($data2[0]);
        if($data3['sticky'] == 'sticky')
            echo '<strong>'.get_the_title().'</strong><br />';
        } else {
            echo ''.get_the_title().'<br />';

I would like to order the query using the Event Start Date from the custom meta box. I have tried this with no luck. Can someone help me with this?


You'll have to save the date under its own meta key to be able to query and order on it. Right now your meta is being serialized into a single string, so MySQL is unable to query on that data.

|improve this answer|||||
  • how would I do that? – BrettAdamsGA Jun 27 '12 at 16:08
  • you'll have to change within your meta box functions to add and update separate keys instead of saving it all as an array under the single _my_meta key. see codex for add_post_meta, get_post_meta and update_post_meta – Milo Jun 27 '12 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.