如何在wp_Query META_QUERY键中获取元数据值

时间:2012-03-01 作者:Chris

如何修改WP\\u查询以获得自定义元框值,而不是像我现在这样的自定义字段值?

我的查询是这样的:

<?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();
?>
更新:(我用来创建元盒的内容)

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; }
    }
}
我有meta\\u事件。php在正确的位置,我可以看到并更新特定页面上的元框,它确实保留了它的值,我可以在任何页面上显示元框的内容,这就是我用来获取元框值的方法:

global $wp_query;   $array = get_post_meta(376,\'_my_meta_events\',TRUE); ?><?php $my_date = $array[\'date\'];
现在,如何修改meta\\u查询键以使用此metabox值过滤页面?

2 个回复
最合适的回答,由SO网友:chrisguitarguy 整理而成

自定义元框以与自定义字段相同的方式存储值,它们只是有一个更漂亮的UI。

除了知道key 自定义元框在其下存储其值。如果您使用插件生成自定义元框,则必须深入其内部以找到元键。大多数自定义元框通过以下划线开头来“隐藏”键,使其不会显示在自定义字段UI中(_).

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

SO网友:Shuhad zaman

如果要搜索多个元数据库,请尝试此操作。这对我有用

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

结束