我们有一个用于处理事件的插件。该插件不处理重复发生的事件,因此我们想添加此功能。
我已经编写了一个查询,该查询正在起作用并提取我们想要的事件。看起来像这样:
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那样进行大型查询?
你是正确的:你应该使用 WP_Query
时与本地WordPress的数据库表时,即使您的自定义信息。然而,WordPress的提供了wpdb
类(你必须阅读本链接)数据库操作,它应该被用来工作,自己的自定义数据库表。
因此,如果您确实要执行该查询,则应使用wpdb的SELECT通用结果,并进行一些调整以将其“转换”为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
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句