好的,所以我使用WPAlchemy class 在写文章页面中创建自定义字段写面板,到目前为止一切都很顺利。。。然而,有一个问题我似乎无法解决。我试图使用“事件日期”的自定义字段值对自定义页面模板上的事件进行排序。
我按照指示做了"Query based on Custom Field and Sorted by Value" 在codex中找到,尝试设置自定义查询,但它似乎不起作用?
以下是“事件”页面的自定义页面模板中的代码:
<?php
/*
Template Name: Events
*/
get_header();
?>
<div id="depthead" class="grid_12">
<h2>Upcoming Events</h2>
</div><!--/depthead-->
<?php
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = \'_events_meta[event_date]\'
AND wposts.post_type = \'post\'
ORDER BY wpostmeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): global $post; $cnt=0; foreach ($pageposts as $post): $cnt++; setup_postdata($post); ?>
<div id="article-<?php echo get_the_ID(); ?>" class="listingbox grid_3">
<div class="deptpostimg">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo $events_metabox->get_the_value(\'event_thumbnail\'); ?>" style="outline:1px solid #000" alt="<?php the_title_attribute(); ?>" /><span class="event-date"><?php $events_metabox->the_value(\'event_date\'); ?></span></a>
</div><!--/deptpostimg-->
<h4 class="listing-titles"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
<div class="excerpt">
<?php the_excerpt(); ?>
</div><!--/excerpt-->
</div><!--/article-<?php echo get_the_ID(); ?>-->
<?php if($cnt % 4 == 0) { ?>
<div class="grid_12 rowseparator">
<hr />
</div><!--/rowseparator-->
<?php } ?>
<?php endforeach; endif; ?>
</div><!--/wrapper-->
<?php get_footer(); ?>
就像它没有拾取自定义字段键一样。。。我用来创建这些自定义写入面板的类将它们全部存储为一个数组,因此我尝试使用以下方法访问它们:_events_meta[event_date]
也许这就是问题所在,但如果它是。。。
有什么想法吗?
EDIT: 这是一幅图像,您可以看到自定义字段是如何存储在数据库中的。希望这能帮你弄明白为什么
_events_meta[event_date]
在查询中不起作用?