Home › Forums › Calendar Products › Events Calendar PRO › Get event ID from within a function for ACF field retrieval
- This topic has 7 replies, 3 voices, and was last updated 10 years, 9 months ago by
Support Droid.
-
AuthorPosts
-
September 3, 2013 at 2:01 pm #64343
corageroux
ParticipantHi there
I’m currently using TEC Pro on a site that is heavily dependent on Advanced Custom Fields. I have used it to add some meta data to the tribe_events post type (along with all the standard WP post types) but unfortunately, I can’t use the get_field function as I normally do EVERYWHERE else.
It seems that TEC uses a post ID of -9999 which is clearly the cause of this function returning false. Declaring the global $post variable and accessing the $post->ID just returns the -9999 instead of the event ID – strange way to create a WordPress plugin, guys.
Anyway, I need to know how to get the event ID from within a function. My WP templates don’t contain code, they simply call a series of functions which handle the markup, but it seems that the tribe_ Template tags are useless from within functions.
1. How do I get the event ID from within a function?
2. How can I use TEC template tags (functions) from within my own functions?I’m guessing there is either a global variable I need to declare or a class I need to instantiate.
Thanks
September 4, 2013 at 2:43 pm #64558corageroux
ParticipantHello? This is a premium forum, right? If someone could shed a little light on this issue, I’d really appreciate the help.
September 4, 2013 at 3:39 pm #64570Barry
MemberHi corageroux,
Apologies for the delay, first of all.
There’s a limit to how much help we can provide with customizations, however I think the problem here comes down to where and when you are trying to obtain the post ID for events.
If you are using the Default Page Template option in Events → Settings → Display for instance then the ‘post’ you are noticing with an ID of -9999 is simply a wrapper or container that is used to deliver our content. Can you provide any more details such as where you are trying to obtain the event ID and perhaps share some code so we can see what you are doing?
Do note the forum doesn’t currently handle directly posted code very well, so it’s best to share it using a service like Pastebin or Gist.
Thanks!
September 4, 2013 at 4:01 pm #64574corageroux
ParticipantHi Barry, thanks for replying.
I am aware that I can select a different template to use as the ‘wrapper’ for TEC, but that won’t help me.
Basically, I’m saving post meta to individual events pages (using the Advanced Custom Fields plugin), and that post meta will be saved to the event ID. The data saves just fine, as I can see it appears in the default WP custom fields meta box. The problem is retrieving that meta data for each event. The -9999 wrapper you mention, applies to ALL pages/events related to TEC. So, when I use the ACF API get_field() function, it tries to find meta data for a post ID of -9999 and, of course, returns false, because the meta data is stored under the event’s ID, not the wrapper’s.
I should mention that the meta field works flawlessly on all other posts, pages, and CPTs, so we can save some time here and bypass the whole debug process.
I really need to know how to get the event ID from within a function that is called from a page template.
The TEC functions in the documentation work when called directly within a page template, but I use a different system. All that resides in my pages templates is one function call that initialises the whole page through a series of function calls handled elsewhere.
Here is a crude example – http://pastebin.com/wkP26wFf
I hope this makes sense? Perhaps this is a question for one of the TEC devs?
Cheers
PhilSeptember 4, 2013 at 6:44 pm #64600Barry
MemberHi Phil:
I am aware that I can select a different template to use as the ‘wrapper’ for TEC, but that won’t help me.
Are you sure that won’t help?
Just to clear up any misunderstanding, the point I’m trying to make is that when something other than the Default Events Template is being used a virtual post (with an ID of -9999) is part of the system used to wrap up and deliver our content.
For instance if you are using the Default Page Template and the context is that of a single event, then if you inspect the global $post object early enough – or indeed use a function like get_the_ID() – it will seem the ID is set to -9999. However, this is not true if the Default Events Template is in use and you would instead see the actual post ID of the single event. In light of which, if it’s possible for you to switch to the Default Events Template perhaps that would be an easy way forward?
If that isn’t possible and/or you are unable to run your code at a later point in the request (when the actual event itself is set up as the current post) you could alternatively inspect the global $wp_query object and its posts property in particular, which in the case of a single event request will contain our virtual wrapper post and also the actual event post.
Does that help at all? I appreciate it’s slightly involved, so if you need further clarification on anything I’ll try my best to help.
September 4, 2013 at 7:17 pm #64606corageroux
ParticipantHi Mate
I hear what you are saying, but using the default events template circumvents everything I worked to deliver for my client. They can dynamically assign sidebars and layout on every other page of the site, so I would like to deliver the same on their events pages. The very nature of this functionality requires the code to run just before the main loop, as it determines the very structure of the page – i.e. it controls the wrapping markup of the page content.
So, I’ll go with the Default Page Template and just grab the data from the $wp_query object.
Thanks for your help, I appreciate it. Getting used to a new plugins behaviour can be such a drama when all you wanna do is get the site live.
Thanks, Barry.
September 5, 2013 at 6:51 am #64665Barry
MemberNo problem, I can relate to that 🙂
Since it’s sounding like you’re all sorted here I’ll go ahead and close this thread. Thanks again!
July 7, 2015 at 6:27 am #980643Support Droid
KeymasterThis topic has not been active for quite some time and will now be closed.
If you still need assistance please simply open a new topic (linking to this one if necessary)
and one of the team will be only too happy to help. -
AuthorPosts
- The topic ‘Get event ID from within a function for ACF field retrieval’ is closed to new replies.
