2

我们有一个处理事件的插件。这个插件不处理重复事件,所以我们想添加这个功能。

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

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 中进行类似的大查询?

4

2 回答 2

1

你是对的:应该 WP_Query 在处理原生 WordPress 的数据库表时使用,即使是你的自定义帖子。尽管如此,WordPress 提供了用于数据库操作的wpdb(您必须阅读此链接),它应该用于处理您自己的自定义数据库表。

因此,如果您真的想执行该查询,您应该使用wpdb 的 SELECT Generic Result并添加一些调整以将其“转换”为 WordPress 循环

<?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
}
?>
于 2013-10-02T23:59:06.800 回答
0

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

于 2013-10-03T14:33:18.720 回答