我正在使用活动日历3。由于似乎没有简单的方法可以将所有即将发生的事件查询为帖子,因此我尝试了这种方法,从DB获取ID并从中查询帖子:
<?php if(!ec3_check_installed(__(\'Upcoming Events\',\'ec3\')))
return;
global $ec3,$wpdb,$wp_version;
$calendar_entries = $wpdb->get_results(
"SELECT
p.id AS id
FROM $ec3->schedule s
LEFT JOIN $wpdb->posts p ON s.post_id=p.id
WHERE p.post_status=\'publish\'
AND end>=\'$ec3->today\' $and_before
ORDER BY start $limit_numposts", OBJECT_K
);
$args = array(
\'post_type\' => \'post\',
\'post__in\' => $calendar_entries,
\'orderby\' => \'id\',
\'order\' => \'DESC\'
);
$query = new WP_Query( $args );
它不起作用,显然是因为数组看起来像这样,如果我var\\u dump它:
array(2) {
[37]=> object(stdClass)#2183 (1) {
["id"]=> string(2) "37"
}
[8]=> object(stdClass)#2186 (1) {
["id"]=> string(1) "8"
}
}
如何使用此数组进行查询?
最合适的回答,由SO网友:birgire 整理而成
在您的$wpdb
查询:
// collect calendar id\'s
$ids = array();
foreach( $calendar_entries as $calendar_entries):
array_push( $ids, $calendar_entries->id );
endforeach;
// query the above calendar id\'s
$args = array(
\'post_type\' => \'post\',
\'post__in\' => $ids,
\'orderby\' => \'id\',
\'order\' => \'DESC\'
);
$query = new WP_Query( $args );
您还可以修改
WP_Query
通过使用
posts_where,
posts_join和
posts_clauses.