Ask your WordPress questions! Pay money and get answers fast! (more info)

Repeat post multiple times in loop WordPress

  • SOLVED

I have a custom post type with events. These events are simple posts with a title and description. In the loop I want to be able to repeat the same post on multiple dates.

For a easy example we have to events

Event A that will occur on October 12th, 15th and 17th
Event B will occur on October 13th.

I create Event A and set the publish date to October 12th. Then I add a meta key called event_date and add october 15th. I repeat this for October 17th. (The date format for event_date is YYYY-MM-DD)

For Event B I just add a title and description and then set publish date to October 13th.


Is it possible to create a query for the loop that will return 4 posts?

In this case:
* Event A
* Event B
* Event A
* Event A

I would prefer that that meta key event_date is only used for posts that occur more than once and all posts are included in the same loop.

Also just to clarify the loop should be sorted by date so a solution where I would get all the posts first and then all posts that have a event_date would not solve my problem.

Answers (3)

2013-10-10

Eric P. answers:

I'll start off with some conceptual discussion for what you probably <strong>could</strong> do here. It sounds like you know how to do the query, write the loop.

What you can do, is during the loop, push the posts into an array with the event date, post ID and post info that you'll be rendering. Then push the same info into the array for each additional "event date." Don't render anything.

Once you've looped through all the posts and pushed them all into the array (multiple times for events with multiple dates), sort the array, and step through the array and actually render the posts (including the duplicate posts).

UPDATE:

First, note, this code is untested and serves as a framework for the solution. It may contain minor errors.

You want something like this.


<?php
$args=array( /* any additional get_posts query args here */
'post_type' => 'event' );
$events_list = $get_posts ( $args );
$events_array = array();
foreach ( $events_list as $event ) {
$key = implode(':', ( mysql2date('Y-m-d',$event->post_date), $event->post_id ) ); // multiple events on same date will be in "post_id" order
$events_array[$key]=$event;
foreach ( $get_post_meta($event->post_id,'event_date') as $event_date ) {
$key = implode(':', ( $event_date, $event->post_id ) );
$events_array[$key]=$event;
}
}
ksort($events_array);
foreach ( $events_array as $key => $event ) {
setup_postdata( $event ); ?>
( $event_date, $event_id ) = explode(':',$key);
/*
* Render the event
* stuff like the_title(), the_content(), the_exerpt(), or whatever is appropriate goes here.
* NOTE: For the date, use $event_date, not the_date() or get_the_date().
*/
}
?>


Eric P. comments:

<strong>@Hariprasad</strong>, your code only does the "repeat" events.

If I understand the opening post, one time events need to show only on the post "publish date" ($post->post_date). Events that repeat have the <strong><em>additional</em></strong> repeat dates in the post meta data as "event_date".

I don't think a single query could get all the dates in the order needed, since they come from different fields in different tables. Even joining posts to post_meta on the post_id wouldn't completely resolve this, because you still wouldn't have a record with "event_date" for one time events. Those only have the post_date.

That's why I suggested processing all the events into an array in a first loop, putting additional entries into the array for the repeat events, sorting the array, then looping through the array entries in the "rendering" loop.

2013-10-10

Hariprasad Vijayan answers:

Hello,

I think this is not possible directly with query posts or wp_query, you can use custom sql query for this.
The code using custom query is


<?php
global $wpdb;
$query = "SELECT post_id,CAST(meta_value as date) as event_date FROM `wp_postmeta` WHERE `meta_key` = 'event_date' order by event_date asc";
$postlist = $wpdb->get_results($query, OBJECT);
?>
<?php if ($postlist): ?>
<?php foreach ($postlist as $post): ?>
<?php $posts = get_post($post->post_id);
$title = $posts->post_title;
$content = $posts->post_content;
?>
<h2><?php echo $title; ?></h2>
<p><?php echo $content; ?></p>
<?php endforeach; ?>
<?php else :
echo 'Not found';
endif; ?>

Let me know if you have any doubt in this

2013-10-11

Fahad Murtaza answers:

I'd second Eric P. on this one!