将自定义创建的帖子对象添加到Wordpress循环中

时间:2013-10-02 18:56:42

标签: php wordpress

我们有一个处理事件的插件。此插件不处理重复发生的事件,因此我们想添加此功能。

我已经编写了一个正在运行的查询,并引入了我们想要的事件。它看起来像这样:

SELECT posts.*, metaStartDate.meta_value as EventStartDate, meta.meta_value as EventRepeats, metaTimeSpans.meta_value as EventTimeRanges
FROM $wpdb->posts posts
LEFT JOIN $wpdb->postmeta meta ON (posts.ID = meta.post_id AND meta.meta_key = '_EventRepeats')
LEFT JOIN $wpdb->postmeta metaStartDate ON (posts.ID = metaStartDate.post_id AND metaStartDate.meta_key = '_EventStartDate')
LEFT JOIN $wpdb->postmeta metaRepeatsUntil ON (posts.ID = metaRepeatsUntil.post_id AND metaRepeatsUntil.meta_key = '_EventRepeatsUntil')
LEFT JOIN $wpdb->postmeta metaTimeSpans ON (posts.ID = metaTimeSpans.post_id AND metaTimeSpans.meta_key = '_EventTimeRanges')
WHERE posts.post_type = 'tribe_events'
AND posts.post_status = 'publish'
AND meta.meta_value != 'once'
AND '$queryDate 00:00:00' BETWEEN metaStartDate.meta_value AND metaRepeatsUntil.meta_value

正如我所提到的,这是获取我们想要的事件,但是如何将它们添加到Wordpress循环中以便have_posts()the_post()之类的东西工作?

编辑:看起来正确的答案是使用WP_Query,但有没有办法在WP_Query中执行类似的大查询?

2 个答案:

答案 0 :(得分:1)

你是对的:应该使用 WP_Query 每当处理原生WordPress的数据库表时,即使你的自定义帖子也是如此。尽管如此,WordPress provides the wpdb class(您必须读取此链接)才能进行数据库操作,用于处理您自己的自定义数据库表。

因此,如果您真的想要执行该查询,则应使用wpdb's SELECT Generic Result并向"convert" it to a WordPress loop添加一些调整:

<?php
// Your custom query
$querystr = "
    SELECT posts.*, metaStartDate.meta_value as EventStartDate, meta.meta_value as EventRepeats, metaTimeSpans.meta_value as EventTimeRanges
    FROM $wpdb->posts posts
    LEFT JOIN $wpdb->postmeta meta ON (posts.ID = meta.post_id AND meta.meta_key = '_EventRepeats')
    LEFT JOIN $wpdb->postmeta metaStartDate ON (posts.ID = metaStartDate.post_id AND metaStartDate.meta_key = '_EventStartDate')
    LEFT JOIN $wpdb->postmeta metaRepeatsUntil ON (posts.ID = metaRepeatsUntil.post_id AND metaRepeatsUntil.meta_key = '_EventRepeatsUntil')
    LEFT JOIN $wpdb->postmeta metaTimeSpans ON (posts.ID = metaTimeSpans.post_id AND metaTimeSpans.meta_key = '_EventTimeRanges')
    WHERE posts.post_type = 'tribe_events'
    AND posts.post_status = 'publish'
    AND meta.meta_value != 'once'
    AND '$queryDate 00:00:00' BETWEEN metaStartDate.meta_value AND metaRepeatsUntil.meta_value
 ";

// Execute the query using wpdb and fetch results as objects
$pageposts = $wpdb->get_results($querystr, OBJECT);

if ($pageposts){
    global $post;
    foreach ($pageposts as $post){

        // Here's the loop magic
        setup_postdata($post);

        // Now you are free to use any common loop functions, such as
        // the_ID();
        // the_permalink();
        // the_title();
        // and all others.
        ?>

        <div class="post" id="post-<?php the_ID(); ?>">
            <h2>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                    <?php the_title(); ?>
                </a>
            </h2>
            <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
            <div class="entry">
                <?php the_content('Read the rest of this entry »'); ?>
            </div>
            <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  
            <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
        </div>
        <?php
    }
}else {
    // No results found
}
?>

答案 1 :(得分:0)

@ turboguy50035您可以使用Tribe Events Calendar插件的内置查询机制挂钩到'pre_get_posts'查询过滤器。虽然在我看来最好的选择就是获得PRO插件,它会在查询中添加重复。