3

我有一个“事件”的自定义帖子类型,并且我有一个事件日期的自定义元框。添加的所有事件都以 srtotime() UNIX 格式记录到数据库中的所有日期。

当查询事件列表时,我需要按周将它们分开。所以..“4/23 周”然后仅列出该周的事件..然后是“4/30 周”,列出该周的事件。

所有这些都需要动态和逻辑检测每周开始的时间,并将其与数据库中保存的返回事件的日期进行比较,从而将它们分组为“周”。

我希望一切都说得通。我只是在寻找此类功能的资源时遇到了麻烦,而且我不确定所需的逻辑以及 WP 可能已经拥有我可以使用的东西。截至目前,正在使用自定义 Wordpress 循环提取结果,但如果需要,我可以使用 SQL。

提前感谢您以任何方式提供帮助!

*更新的代码

<?php
$loop = new WP_Query(array(
'post_type'         => 'tf_events', 
'posts_per_page'    => -1, 
'orderby'           => 'meta_value', 
'meta_key'          => 'tf_events_startdate', 
'order'             => 'ASC'
));

echo '<pre>';
print_r($loop);
echo '</pre>';

$groups = array();

if ($loop->have_posts()) {
  while ($loop->have_posts()) {
    $loop->the_post();

    $groups['Week of ' . $post->post_date][] = $post;

// Get event dates from WP metabox data (returns as string) 
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data
$longweekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data

// Reformat dates
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate); 
$prettyweekof = date("m/d", $longweekof);

// Get Custom Meta Terms/Catagories/taxonomies
$the_venue = get_the_term_list(  $post->ID, 'venue' );
$event_age = get_the_term_list(  $post->ID, 'event_age' );

// Get Custom Metabox Data - URL for Buy Tickets
$buytix = get_post_meta( $post->ID, 'buytix', true );
 }
}
?>

<h1>Groups of posts</h1>
<?php echo '<pre>'; print_r($groups); echo '</pre>';?>
<?php foreach ($groups as $week => $rows) : ?>
<h2><?php echo $week ?></h2>

<ul>
    <?php foreach ($rows as $post) : setup_postdata($post) ?>
    <?php echo ' <h1>POST START</h1> <pre>';
print_r($post);
echo '</pre> <h1>POST END</h1>'; ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a> <?php echo $prettystartdate; ?></li>
    <?php endforeach ?>
</ul>
<?php endforeach ?>
4

2 回答 2

0

当我需要对元素进行分组时,我通常使用关联数组,其中键是分组依据。所以在你的情况下,数组看起来像:

array(
    'Week of 4/30' => array(array of posts)
);

一个简单的方法(一旦你有所有的帖子)是这样的:

$groups = array();

foreach ($posts as $post) {
    $groups['Week of ' . date('m/d', $post->pub_date)][] = $post;
}

然后你可以:

foreach ($groups as $title => $posts) {
     echo "<h2>$title</h2>";

     foreach ($posts as $post) ...
}

编辑:这是更完整的代码(包括 WP 查询等)的样子:

<?php
# Get all posts
$loop = new WP_Query(array(
    'post_type'         => 'tf_events', 
    'posts_per_page'    => -1, 
    'orderby'           => 'meta_value', 
    'meta_key'          => 'tf_events_startdate', 
    'order'             => 'ASC'
));
$groups = array();

# Create groups of posts
if ($loop->have_posts) {
    while ($loop->have_posts()) {
        $loop->the_post();

        $groups['Week of ' . date('m/d', $post->tf_events_weekof)][] = $post;
    }
}
?>

<h1>Groups of posts</h1>

<?php foreach ($groups as $week => $rows) : ?>
    <h2><?php echo $week ?></h2>

    <ul>
        <?php foreach ($rows as $post) : setup_postdata($post) ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
        <?php endforeach ?>
    </ul>
<?php endforeach ?>
于 2012-04-22T16:43:05.243 回答
0

这是对我有用的粗略解决方案。

<?php
$loop = new WP_Query(array(
'post_type'         => 'tf_events', 
'posts_per_page'    => -1, 
'orderby'           => 'meta_value', 
'meta_key'          => 'tf_events_startdate', 
'order'             => 'ASC'
));

$groups = array();
$heading = '';
if ($loop->have_posts()) {
while ($loop->have_posts()) {
    $loop->the_post();

//set up pretty week
$weekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data

//format dates
$prettyweekof = date("m/d", $weekof);
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate); 

//set info on objects
$post->prettyweekof = $prettyweekof;
$post->pretty_startdate = $prettystartdate;
$post->pretty_enddate = $prettyenddate;

//Echo heading when need to
if($post->prettyweekof !== $heading)
{
$heading = $post->prettyweekof;
echo '<h2>'.$heading.'</h2>';

}

//list events
echo $post->post_title.' | '.$post->pretty_startdate.' - '.$post->pretty_enddate.'<br />';


}
}
?>
于 2012-04-24T03:17:40.207 回答