I run an events company whose website uses a custom post type system for posts (blog), events and tickets all stored as CPT’s in the DB.
On the guestlist (which counts ‘ticket’ custom posts if the event [post] number == current post) I would like to implement a “New Customer” tag to prompt the host to look out for them.
There are two problems with this: you cannot search for the oldest ‘ticket’ post, as they may not have bought them in chronological order, and you cannot get all tickets and order by post number as they may not have been added to the site in chronological order either.
The two ways I can think of getting around this is either:
(1)
By multiple search arrays, i.e. query TICKET > to get USER > get all TICKETS for USER NUMBER > get EVENT [POST] NUMBERS from those tickets (stored as meta) > get EVENTS by post number > – SORT ARRAY BY DATE of event > IF FIRST EVENT IN THAT ARRAY == CURRENT EVENT [post] NUMBER echo NEW CUSTOMER
This seems to be an awfully long way around and I am a little reluctant to try and make all that work as a relatively novice coder.
(2) Somehow retrospectively adding a ‘date of event’ meta data tag to all ticket posts and then I can directly query them…. Much easier, but I am unsure how to then re-translate that 010214 so that the query recognises it as a date rather than a number…
Number (2) would be ideal but I can’t find any information as to how to add all this data retrospectively, and then for future posts – all the data is there in the system but all searches for information lead me to people talking about going back and batch adding search engine meta, not WordPress post meta.
I started coding Number (1) and realised the mountain ahead of me and stopped – if anyone would like that portion of the code though, I can still post it…
Thanks in advance, sorry for the long question – trying to equip any helpers with as much information as possible.
EDIT:
A way around this could be actually to use the event page itself as each$event
and$ticket
are called there.
Structure could be:
write meta to `$ticket`
with
meta key = 'event_date'
value = $eventDay [= date( 'dmy', $event->start );]
Then a simple query to find all user tickets, and order by this new meta tag ‘event_date’.
If first entry event number == current post, BINGO!
This will query all the ticket posts and update a
_ticket_date
field. However, I’m not yet sure how to retrieve the actual date, so you’ll have to provide me with a little more information for that. Assuming it works properly I would run the following once, and then remove. You could achieve a similar effect by using setting a transient but I won’t get into that now.Additionally, to save the meta whenever a ticket is saved you can add an action to
save_post-{post-type}
. This will still leave you some coding to do if you change the event’s date meta field.So,
I got round this by actually putting the function to add the meta data on the event pages themselves (in event-single.php). This means that you are not held by the date of the event at the time of the ticket being created should, for example, you change the date of the event. Each time the page is accessed the date is transferred to the tickets in question.
I stored the date metadata in yymmdd format so in fact if you order them numerically it still produces the correct result without having to re-translate into dates.
(This may not be perfect – I am a novice coder and have taught myself php through the necessity of maintaining a wordpress website)
So this is what I did.