This is a really nuanced case but I thought it might be worth trying to generalize for a post for anyone who may be working with future events with Modern Tribe’s Events Calendar.
Say you’re setting up some recurring event and within the body of the event (or, more appropriately, the post_content) you want to say something like:
It’s been N-years since occurred.
The thing is, each subsequent event will obviously be one year greater than the one before it. So you can’t necessarily hardcode the value into the events without it being placed into the post content of every event.
So what’s one way to tackle this?
Updating Subsequent Events
As with most things, this is going to depend on how your codebase is set up, but assuming you’re working with series events, it’s not too bad.
For this post, I’m assuming:
- you have events set up in such a way that you have the first event in a series, and then you have subsequent events),
- you’re comfortable running basic queries using $wpdb,
- you’re comfortable with basic regular expressions,
- you’re familiar with wp_update_post
The general process by which you can update each event to reflect the proper number of years within its post_content will go something like this:
- using the first event in the series, find all subsequent posts,
- locate the number in the post_content and increment it by one,
- update the post.
Pretty straightforward, right? Here’s how to do it.
1. Find Subsequent Events
To update subsequent events, it’s important to have the ID of the main event. This is usually easy to obtain if you’ve already programmatically created the event because tribe_create_event returns the post ID.
Otherwise, you can do something like finding a post by its title or something similar. And once you have that post, you can grab its ID.
Once done, write a quick query to retrieve all of the posts with the initial post ID as its parent:
Obviously, I prefer to put this into its function so I can call it multiple places throughout the application, debug it as needed, and to isolate its responsibility.
You may want to return the results as a collection of objects or add more qualifiers to the query to limit the type of results that are returned. Regardless, the main takeaway from this code is that you’re grabbing the IDs and post_content of all subsequent posts in a series based on the ID of the initial event.
2. Update The Post Content
Once the events have been returned, a loop can be set up to iterate through them. Depending on how you define the loop will determine how you set up a counter, but one of the most basic ways to do this is to:
- define a value, starting at one, that you can add and increment during each iteration,
- add this to the digit found in the post.
So if you were to use a foreach loop, the code might look something like this:
This will be a little more complicated if you have multiple digits in the body of your post, but this is enough to give the idea of how to work with at least one (like in the example at the top of the post).
3. Update The Post
Finally, once the post_content has been updated, it’s a matter of really just calling a WordPress API to update the post content.
Of course, as mentioned throughout the post, your code may look different, the performance aspects of this may be different or require some other considerations (which I’ll cover momentarily).
But, if nothing else, this is at least a starting point.
There may be times in which the post content contains multiple numbers, or there may be times in which there may be thousands of subsequent events. In each of these cases, the above approach may not fit the bill.
That is, you can still follow these steps, but they may not be the most performant as written. In that case, you may need to set up a batch process to handle it or do it during off hours using a cron job, or one of several other ways to tackle the problem.
The aforementioned approaches are beyond the scope of this post, but the three-step approach remains the same. It’s more about divide-and-conquer to tackle the problem than how it gets done.