3

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:

<?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?

2
  • 1
    What are you using to generate the custom meta boxes? Commented Mar 1, 2012 at 19:25
  • I have updated my question with the code used to create the meta boxes. Commented Mar 6, 2012 at 12:22

2 Answers 2

3

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'
) );
4
  • I used to have an array with the key with the metaboxes I've done so far, that's why I was asking. If the key will take an array as a value, it will be fantastic. I will try it right now. Commented Mar 1, 2012 at 19:45
  • I don't think you can use an array as a key? I'm not really clear on what you mean by this: "an array with the key with the metaboxes" Commented Mar 1, 2012 at 20:37
  • This is what I use to get meta values: <?php global $wp_query; $array = get_post_meta(376,'_my_meta_events',TRUE); ?><?php $my_date = $array['date']; ?> and I've tried to use a variable containing my array value for the key of my meta_query, doesn't work. Commented Mar 1, 2012 at 22:24
  • I have accepted the answer as it's obvious the answer is the right one based on my question but my problem is still not solved. Anyone have any idea how to achieve that? Commented Mar 12, 2012 at 12:59
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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