Using a Meta value on Post Title

I saw a tweet earlier today of a user asking how to solve common issue, how to use a single variable inside of the title of my WordPress posts, in this case events.

How to do it

To understand how to do this task you first need to see that these kind of variables can be used inside of title without been inserted manually on by applying a little concept called filter, which will allow you to hook to a WordPress method and apply X or Y function to solve your issue.

I will first exemplify how to approach this code with a simple example and then I will show the snippet to solve the issue on the WooCommerce Ticket plugins.

function bord_time_on_title( $title, $post_id ){
    // Using the title of the post and the variable $post_id I will get the Time of
    // the post and append to the end of the post Title
    return $title . ' | ' . get_the_time( '', $post_id );
}

// Adds the filter to the correct hook
add_filter( 'the_title', 'bord_time_on_title', 10, 2 );

The code above is only the function that you will do the filtering for you, you can add this to your theme's function.php file.

Answering Josh's Question

On this case we will grab the value of total available tickets on that event and append to the end of every event that belongs to the Post Type Event, below you can see the snippet that will do that.

add_filter( 'the_title', 'bord_ticket_available_on_title', 10, 2 );

function bord_ticket_available_on_title( $title, $post_id ){
    $quantity = bord_get_tickets_available( $post_id );
    if ( false === $quantity ){
        // If we are not dealing with an event
        return $title;
    } elseif ( 0 === $quantity ) {
        // If there are no tickets available
        return $title . ' - No tickets left';
    } elseif ( 1 === $quantity ) {
        // Only one ticket
        return $title . ' - Last Ticket!';
    } else {
        // X amount of tickets
        return $title . ' - ' . $quantity . ' tickets left';
    }
}

function bord_get_tickets_available( $post_id ) {
    $post = get_post( $post_id );

    // If we are not dealing with an Event we just return
    if ( TribeEvents::POSTTYPE !== $post->post_type ){
        return false;
    }

    if ( ! class_exists( 'TribeWooTickets' ) ){
        return false;
    }

    // Get all the tickets Ids
    $tickets_ids = TribeWooTickets::get_instance()->get_tickets_ids( $post->ID );

    $qty = 0;

    foreach ( $tickets_ids as $ticket_id ) {
        // Grab the real Ticket instance
        $ticket = TribeWooTickets::get_instance()->get_ticket( $post->ID, $ticket_id );

        // Check for a simple version of the WooCommerce Product class otherwise load the full one
        if ( class_exists( 'WC_Product_Simple' ) ) {
            $product = new WC_Product_Simple( $ticket->ID );
        } else {
            $product = new WC_Product( $ticket->ID );
        }

        // Deal with the possible Timezone
        $gmt_offset = ( get_option( 'gmt_offset' ) >= '0' ) ? ' +' . get_option( 'gmt_offset' ) : " " . get_option( 'gmt_offset' );
        $gmt_offset = str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), $gmt_offset );

        // Check for the event End date
        $end_date = null;
        if ( ! empty( $ticket->end_date ) ){
            $end_date = strtotime( $ticket->end_date . $gmt_offset );
        } else {
            $end_date = strtotime( tribe_get_end_date( get_the_ID(), false, 'Y-m-d G:i' ) . $gmt_offset );
        }

        // Grab the event Start
        $start_date = null;
        if ( ! empty( $ticket->start_date ) ) {
            $start_date = strtotime( $ticket->start_date . $gmt_offset );
        }

        // Compare the dates, if it's still in time check for more
        if ( ( empty( $start_date ) || time() > $start_date ) && ( empty( $end_date ) || time() < $end_date ) ) {

            // Check if the product has stock
            if ( $product->is_in_stock() ) {
                // Max quantity will be left open if backorders allowed, restricted to 1 if the product is
                // constrained to be sold individually or else set to the available stock quantity
                $max_quantity = $product->backorders_allowed() ? 0 : $product->get_stock_quantity();
                $max_quantity = $product->is_sold_individually() ? 1 : $max_quantity;

                // Add the stock to the total quantity
                $qty += (int) $max_quantity;
            }
        }
    }

    return $qty;
}

The snippet above will only work with WooCommerce Tickets if you are using any of the other Ticket add-ons by Tribe this won't work.

Questions?

If you have any question in regards to how to approach any of the code above, don't be afraid to post in the comment section down below and I will try to answer it.

Discussions — 2 Responses

  • Josh L. Sullivan (@joshlsullivan) January 18, 2015 on 10:54 pm

    Gustavo, I tried your code but it doesn't work on the site. It erased the Menu on the home page and made all the events disappear from the calendar. I deactivated The Events Calendar Pro too. Any suggestions?

    Reply
    • Gustavo Bordoni Josh L. Sullivan (@joshlsullivan) January 19, 2015 on 9:31 am

      I've updated the code, I forgot to change the function name on the filter, my Bad.

      Reply