使用数组中的类ID进行查询

时间:2013-07-22 作者:Sebastian Starke

我正在使用活动日历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" 
    } 
} 
如何使用此数组进行查询?

1 个回复
最合适的回答,由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_joinposts_clauses.

结束

相关推荐

仅显示特定页面ID的标题和简短摘要

$id = 25; $post = get_page($id); $title = apply_filters(\'the_title\', $post->post_title); $content = apply_filters(\'the_content\', $post->post_content); echo $title, $content; 上面的代码显示了标题和全部内容,但我只想显示简短的摘要。我该怎么做?