如何查询两个元字段并显示它们之间的结果

时间:2011-12-13 作者:Sosthenes Kwame Boame

我正在制作一个搜索表单,在两个元框值内搜索帖子,并在它们之间显示结果。

我有这套

$prefix = \'ghes_\'; // start with an underscore to hide fields from custom fields list
add_filter( \'ghes_meta_boxes\', \'ghes_sample_metaboxes\' );
function ghes_sample_metaboxes( $meta_boxes ) {
    global $prefix;
    $meta_boxes[] = array(
        \'id\' => \'event_meta\',
        \'title\' => \'Event Metabox\',
        \'pages\' => array(\'event\'), // post type
        \'context\' => \'normal\',
        \'priority\' => \'high\',
        \'show_names\' => true, // Show field names on the left
        \'fields\' => array(

        array(
                \'name\' => \'Event Start Date\',
                \'desc\' => \'field description (optional)\',
                \'id\' => $prefix . \'event_start_timestamp\',
                \'type\' => \'text_date_timestamp\'
            ),                  
            array(
                \'name\' => \'Event End Date\',
                \'desc\' => \'field description (optional)\',
                \'id\' => $prefix . \'event_end_timestamp\',
                \'type\' => \'text_date_timestamp\'
            )                   
如果我想在“事件开始日期”和“事件结束日期”之间搜索帖子,查询会是什么样子。

谢谢

1 个回复
SO网友:Ijaas

您需要使用meta_query 类似这样的变量:

$start = time(); //Now
$end= strtotime(\'+1 month\'); //1 month ahead

query_posts(
    \'post_type\' => \'event\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => \'10\',
    \'meta_query\' => array(
        array(\'key\' => $prefix.\'event_start_timestamp\', \'value\' => $start, \'compare\' => \'>=\', \'type\' => \'NUMERIC\'),
        array(\'key\' => $prefix.\'event_end_timestamp\', \'value\' => $end, \'compare\' => \'<=\', \'type\' => \'NUMERIC\')
    )
);

结束

相关推荐

如何修复我的旧主题以便正确加载jQuery

我有一个旧主题是“手动”加载jQuery,这与所有尝试使用jQuery的插件冲突。主题标题中包含以下行:<script type=\"text/javascript\" src=\"<?php bloginfo(\'template_url\'); ?>/js/jquery-1.3.2.min.js\"></script> <script type=\"text/javascript\" src=\"<?php bloginfo(\'template