Template tag to check if current user has submitted an event

Home Forums Calendar Products Community Events Template tag to check if current user has submitted an event

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #994507
    Ryan
    Participant

    Hey Guys,

    Just wanted to see if there was a template tag, or a simple way, to check if a logged in user has submitted an event, or linked with any events. I’ve looked through all the template tag files and don’t see anything close to what I am looking for.

    Reason being – I would like to add a link on our customized account page to view ‘My Events’ but only want the link showing for users who actually have events associated with their accounts.

    Cheers!

    Matt

    #994598
    George
    Participant

    Hey Matt,

    There is unfortunately not a simple, straightforward template tag for this at this time 🙁

    The best solution I can think of would be some custom coding. While custom code is outside the scope of our support forums, the general idea of how to achieve this would be to use tribe_get_events() or WP_Query to query for events with author parameters for the currently-logged-in user.

    If the number of events by the author are more than zero, then this would of course mean that the user had authored at least one event, so you could check against this value and show or hide the link based on that.

    To learn more about tribe_get_events(), check out our knowledgebase article for it here → https://theeventscalendar.com/knowledgebase/using-tribe_get_events/

    To learn more about WP_Query directly, which is what tribe_get_events() ultimately calls, check out the article for it on the WordPress Codex here → http://codex.wordpress.org/wp_query

    Best of luck with this! Let me know if there’s anything else I can help with.

    Cheers,
    George

    #994688
    Ryan
    Participant

    Hey George,

    Thanks for the guidance – that did the trick. Here is the final snippet:

    <?php
    // Check if current user has any events
    $current_user = wp_get_current_user();
    $events = tribe_get_events( array(
         'posts_per_page' => -1,
         'author__in' => $current_user->ID,
         // Include publish and draft so users who submit event can edit events prior to publication
         'post_status' => array('publish', 'draft'),
    ) );
    // Check to see if the events query has results and if so, show the menu item
    if( !empty( $events ) ) : ?>
         <div class="account-toggle-item"><a href="<?php echo get_home_url(); ?>/events/community/list">My Submitted Events</a></div>
    <?php endif; ?>

    A simple check for the current user’s ID to run in the query and set the post_status to both draft and public to ensure it works for all events even if they haven’t been posted yet and we’re all set!

    Thanks,

    Matt

    #994813
    George
    Participant

    Looks good Matt! Thanks for posting your solution and such. Best of luck with your site 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Template tag to check if current user has submitted an event’ is closed to new replies.