How to grab metabox value in wp_query meta_query key

How would I modify my WP_Query in order to get a custom meta box value instead of a custom field one like I currently do?

This is how my query looks like:

Read More
<?php $today = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));

$the_query = new WP_Query(  array ('showposts' => 10,
                'post_type' => 'page',
                'meta_query'=> array(
                    array(
                      'key' => 'start_date',
                      'compare' => '<=',
                      'value' => $today,
                      'type' => 'DATE',
                    )),
                'meta_key' => 'start_date',
                'orderby' => 'meta_value',
                'order' => 'DESC'
                                )
                        );

while ( $the_query->have_posts() ) : $the_query->the_post();
?>

Update: (what I’m using to create the metabox)

function my_meta_init_events() {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if ($post_id == '376') {
    foreach (array('post','page') as $type) {
        add_meta_box('my_all_meta_events', 'Start date', 'my_meta_setup_events', $type, 'normal', 'high'); }
    }
        add_action('save_post','my_meta_save_events'); }
function my_meta_setup_events() {
    global $post;
    $meta = get_post_meta($post->ID,'_my_meta_events',TRUE);
     include(MY_THEME_FOLDER . '/inc/meta_events.php');
    echo '<input type="hidden" name="my_meta_noncename_events" value="' . wp_create_nonce(__FILE__) . '" />'; }

function my_meta_save_events($post_id) {
    if (!wp_verify_nonce($_POST['my_meta_noncename_events'],__FILE__)) return $post_id;
    if ($_POST['post_type'] == 'page') {
        if (!current_user_can('edit_page', $post_id)) return $post_id; }
    $current_data = get_post_meta($post_id, '_my_meta_events', TRUE);  
    $new_data = $_POST['_my_meta_events'];
    my_meta_clean_events($new_data); 
    if ($current_data)  {
        if (is_null($new_data)) delete_post_meta($post_id,'_my_meta_events');
        else update_post_meta($post_id,'_my_meta_events',$new_data); }
    elseif (!is_null($new_data)) {
        add_post_meta($post_id,'_my_meta_events',$new_data,TRUE); }
    return $post_id;
}
function my_meta_clean_events(&$arr) {
    if (is_array($arr)) {
        foreach ($arr as $i => $v) {
            if (is_array($arr[$i])) {
                my_meta_clean_events($arr[$i]);

                if (!count($arr[$i])) {
                    unset($arr[$i]); }
            } else {
                if (trim($arr[$i]) == '') {
                    unset($arr[$i]); }
            }
        }
        if (!count($arr)) { 
            $arr = NULL; }
    }
}

I have meta_events.php in the right place, I see and update the metabox on that particular page, it does retain it’s value, I am able to display the content of the meta box on any page, this is what I am using to grab the value of my metabox:

global $wp_query;   $array = get_post_meta(376,'_my_meta_events',TRUE); ?><?php $my_date = $array['date'];

Now, how can I modify my meta_query key to use this metabox value to filter the pages?

Related posts

Leave a Reply

2 comments

  1. Custom meta boxes store values in the same way as custom fields, they just have a prettier UI.

    You shouldn’t need to modify your query in anyway other than knowing the key under which the custom meta box stores it’s values. If you’re using a plugin to generate the custom meta box, you’ll have to dig into its internals to find the meta key. Most custom meta boxes “hide” the keys from showing up in the custom fields UI by starting them with an underscore (_).

    <?php
    $the_query = new WP_Query(  array (
        'showposts' => 10,
        'post_type' => 'page',
        'meta_query'=> array(
            array(
                'key' => 'start_date', // this key will change!
                'compare' => '<=',
                'value' => $today,
                'type' => 'DATE',
            )
        ),
        'meta_key' => 'start_date',
        'orderby' => 'meta_value',
        'order' => 'DESC'
    ) );
    
  2. try this if you want to search multiple metabox . it worked for me

    $args = array(
                'post_type' => 'teams',
                'meta_query'  => array(
                'relation' => 'OR',
                array(
                    'key' => 'teamMember_skype',
                    'value' => 'your search terms here'
                ),
                array(
                    'key' => 'teamMember_pinterest',
                    'value' => 'your search terms here'
                )
            )
            );
    $teams = new WP_Query( $args );