Help Desk

Showing total number of submitted or upcoming events

  • Posts: 2 Topics: 1
    | Permalink

    I’ve purchased Community Events and I’m looking to display either the total number of submitted events (including past ones) OR the total number of upcoming events on the Events list page. I’ve gone through the theme development and query documentation as well as the knowledgebase but can’t find how to get this count. Can you help?

    Posts: 6978 Topics: 2
    | Permalink

    Hi Amanda,

    What a great question! Thanks for posting it!

    I believe you could count all the posts / events with the wp_count_posts() function of WordPress. (link)

    Something like this:

    $count_events = wp_count_posts( 'tribe_events' );
    $published_events = $count_events->publish;

    Showing / counting only future events would require a more complicated query. You probably could use something like this as a start (check Method 2):

    http://www.wpbeginner.com/wp-tutorials/how-to-list-future-upcoming-posts-in-wordpress/

    Hope this helps.

    Cheers,
    Andras

    Posts: 2 Topics: 1
    | Permalink

    Great! Thanks, Andras! First one works beautifully. Here’s the function I wrote for my theme’s functions.php file:

    function fr_tribe_events_count() {
    global $wpdb;
    $count_events = wp_count_posts( 'tribe_events' );
    $published_events = $count_events->publish;
    echo "
    <h2>Total approved events: "; print_r($published_events);
    echo "</h2>
    ";
    }
    add_action( 'tribe_events_after_the_title', 'fr_tribe_events_count' );

    And then I added the code

    <?php fr_tribe_events_count(); ?>

    to contents.php in wp-content\themes\mytheme\tribe-events\list. I now get a total at the top of my Events list for all events ever approved and published.

    But that second tutorial isn’t going to get me where I need to go to get a count of published Upcoming Events, though (which I’d rather have), because in WordPress “scheduled” posts just have a future date set in the post_date column in the table wp_posts, whereas The Events Calendar manages the list of Upcoming Events with a meta_key called _EventStartDate in the table wp_postmeta. That means I’m going to have to figure out some kind of join, which I’m not the best at. Any chance of a bit more help?

    I’d love this to be a feature in a future release, by the way! I think a lot of people would like to display the total number of Events or Upcoming Events.

    • This reply was modified 9 months, 3 weeks ago by  Amanda. Reason: code display
    • This reply was modified 9 months, 3 weeks ago by  Amanda. Reason: code display
    • This reply was modified 9 months, 3 weeks ago by  Andrs.
    • This reply was modified 9 months, 3 weeks ago by  Andrs.
    Posts: 6978 Topics: 2
    | Permalink

    Awesome job there Amanda! Congrats!

    As for the second one, you hit the nail on the head with the postmeta.

    This might help you go further: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

    Also here’s a sample snippet which I just found there to get you started:

    $args = array(
        'post_type'    => 'tribe_events',
        'meta_key'     => '_EventStartDate',
        'meta_value'   => date( "Y-m-d H:i:s" ), // change to how "event date" is stored
        'meta_compare' => '>',
    );
    $query = new WP_Query( $args );

    Or something like this. 🙂

    Cheers,
    Andras

    Posts: 2 Topics: 1
    | Permalink

    Thanks so much, Andras! Much appreciated! I’ll give that a shot.

    Posts: 6978 Topics: 2
    | Permalink

    I hope that will work out for ya!

    Since this is marked resolved I am going to close this ticket, but if you need anything else related to this topic or another please create a new ticket and we’ll be happy to help.

    Cheers,
    Andras

    PS: If you like our plugins, and you didn’t yet do so 🙂 we would be happy to receive a review in the wordpress.org repository. Thanks!
    https://wordpress.org/support/view/plugin-reviews/the-events-calendar/
    https://wordpress.org/support/view/plugin-reviews/event-tickets/

    PS2: We’d be also grateful if you would give us feedback on your satisfaction with support. Just click on one of the classy looking emojis below. 🙂 If you can spare a few words, that’s even better. Doublethanks!

     

    | Permalink

The topic ‘Showing total number of submitted or upcoming events’ is closed to new replies.