How to grab metabox value in wp_query meta_query key

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'
) );

Leave a Comment