Support » Themes and Templates » PHP – Query for custom post type not displaying // creating theme event list

  • Hey! So I’m trying to adapt an event list template from ‘Clubber’ WordPress theme to ‘Applause’. It is an event list that uses the time and date of each event to sort into ‘UPCOMING EVENTS’ and ‘PAST EVENTS’. This is what I have so far:

    http://www.mybeatfix-new.com/event-list-2/

    The custom post types don’t seem to be registering and displaying. It’s obviously thinking there are “no events coming up”. Check out my code below. Remember I’ve adapted the code from another theme and stuck it onto this one. Is there anything clearly (or not so clearly) wrong in my code below?

    THIS IS THE CODE FOR THE EVENTS LIST TEMPLATE PAGE:

    <?php
    /*
    Template Name: Event LIst
    */
    ?>
    
    <?php
    global $bub_mt;
    get_header('mt');
    ?>
    
    <div class="fixed">
      <div class="col-blog-archive">
        <div class="event-upcoming"><h3>Upcoming Events</h3></div>
    <?php
    // Build a custom query to get posts from future dates.
    $term     = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
    $query    = array(
        'post_type' => 'events',
        'orderby' => 'meta_value',
    	'order' => 'asc',
    	'posts_per_page' => 10,
    	'meta_value' => strftime("%Y/%m/%d", time()- (60 * 60 * 24) ),
    	'meta_key' => 'event_date_interval',
    	'meta_compare' => '>',
        'taxonomy' => 'events',
    	'term' => $term->slug
    );
    
    $wp_query = new WP_Query($query);
    $results = $wp_query->post_count;
    if ($results != ''):
        while ($wp_query->have_posts()):
            $wp_query->the_post();
            global $post;
     		setup_postdata($post);
    		$results = $wp_query->post_count;
    
            $data_event     = get_post_meta($post->ID, 'event_date_interval', true);
            $time           = strtotime($data_event);
            $m_date = get_post_meta( $id, '_cmb_e_date', true );
    		$m_month = get_post_meta( $id, '_cmb_e_month', true );
    		$m_day = get_post_meta( $id, '_cmb_e_day', true );
    		require('includes/language.php');
            $m_start_time = get_post_meta( $id, '_cmb_e_start_time', true );
    		$m_end_time = get_post_meta( $id, '_cmb_e_end_time', true );
            $m_venue = get_post_meta( $id, '_cmb_e_venue', true );
    		$event_text     = get_post_meta($post->ID, "_cmb_e_details", true);
            $custom         = get_post_custom($post->ID);
            $event_ticket   = $custom["_cmb_e_buy_url"][0];
            $image_id       = get_post_thumbnail_id();
            $cover          = wp_get_attachment_image_src($image_id, 'event-cover-arc');
            echo '
        <div class="event-archive">
          <div class="event-arc-data">
            <div class="event-arc-day">' . $m_date . '</div>
            <div class="event-arc-month">' . $m_month . '</div>
          </div><!-- end #event-arc-data -->    
    
          <div class="event-arc-cover">';
            if ($image_id) {
                echo '
            <a href="' . get_permalink() . '"><img src="' . $cover[0] . '" alt="' . get_the_title() . '" /></a>';
            } else {
                echo '
            <a href="' . get_permalink() . '"><img src="' . get_template_directory_uri() . '/images/no-featured/event-single.png" alt="no image" /></a>';
            }
            echo '
          </div><!-- end #event-arc-cover -->
          <div class="event-arc-text">
            <h2 class="event-arc-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>
              <div class="event-arc-info">';
                if($venue) {
                    echo '<p class="event-arc-venue">' . $venue . '</p>';
                }
                if (get_post_meta($post->ID, 'event_allday', true) == 'yes'){
                    echo '<p class="event-arc-time">All Day</p>';
                } elseif ($tstart) {
                    echo '<p class="event-arc-time">' . $tstart . '';
                } if ($tend) {
                    echo ' – ' . $tend . '</p>';
                }
            echo '
              </div><!-- end #event-arc-info -->';
                echo ' ' . the_excerpt_max(165) . ' ';
    
    	if (get_post_meta($post->ID, 'event_disable', true) == 'no') {
            if ($event_text) {
                echo '
                            <div class="event-tickets"><a href="' . $event_ticket . '" target="_blank">' . $event_text . '</a></div>';
            } else {
                if (get_post_meta($post->ID, 'event_out', true) == 'yes') {
                    echo '
                            <div class="event-cancel-out"><p>Sold Out</p></div>';
                } elseif (get_post_meta($post->ID, 'event_cancel', true) == 'yes') {
                    echo '
                            <div class="event-cancel-out"><p>Canceled</p></div>';
                } elseif (get_post_meta($post->ID, 'event_free', true) == 'yes') {
                    echo '
                            <div class="event-cancel-out"><p>Free Entry</p></div>';
                } else {
                    echo '
                            <div class="event-tickets"><a href="' . $event_ticket . '" target="_blank">Buy Tickets</a></div>';
                }
            }
    	}
    
            echo '
          </div><!-- end #event-arc-text -->
        </div><!-- end #event-archive -->';	
    
    	endwhile;
    
    else :
    echo '<h4>Sorry, no events coming up.</h4>';
    endif;
    ?>
    
      </div><!-- end .col-blog-archive -->
      <div class="col-blog-archive">
        <div class="event-past"><h3>Past Events</h3></div>
    
    <?php
    $term     = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
    $query    = array(
        'post_type' => 'events',
        'orderby' => 'meta_value',
    	'order' => 'desc',
    	'posts_per_page' => 10,
    	'meta_value' => strftime("%Y/%m/%d", time()- (60 * 60 * 24) ),
    	'meta_key' => 'event_date_interval',
    	'meta_compare' => '<',
        'taxonomy' => 'events',
    	'term' => $term->slug
    );
    $wp_query = new WP_Query($query);
    $results = $wp_query->post_count;
    if ($results != ''):
        while ($wp_query->have_posts()):
            $wp_query->the_post();
            global $post;
     		setup_postdata($post);
    		$results = $wp_query->post_count;
            $data_event     = get_post_meta($post->ID, 'event_date_interval', true);
            $time           = strtotime($data_event);
            $m_date = get_post_meta( $id, '_cmb_e_date', true );
    		$m_month = get_post_meta( $id, '_cmb_e_month', true );
    		$m_day = get_post_meta( $id, '_cmb_e_day', true );
    		require('includes/language.php');
            $m_start_time = get_post_meta( $id, '_cmb_e_start_time', true );
    		$m_end_time = get_post_meta( $id, '_cmb_e_end_time', true );
            $m_venue = get_post_meta( $id, '_cmb_e_venue', true );
    		$event_text     = get_post_meta($post->ID, "_cmb_e_details", true);
            $custom         = get_post_custom($post->ID);
            $event_ticket   = $custom["_cmb_e_buy_url"][0];
            $image_id       = get_post_thumbnail_id();
            $cover          = wp_get_attachment_image_src($image_id, 'event-cover-arc');
            echo '
        <div class="event-archive">
          <div class="event-arc-data">
            <div class="event-arc-day">' . $m_date . '</div>
            <div class="event-arc-month">' . $m_month . '</div>
          </div><!-- end #event-arc-data -->    
    
          <div class="event-arc-cover">';
            if ($image_id) {
                echo '
            <a href="' . get_permalink() . '"><img src="' . $cover[0] . '" alt="' . get_the_title() . '" /></a>';
            } else {
                echo '
            <a href="' . get_permalink() . '"><img src="' . get_template_directory_uri() . '/images/no-featured/event-single.png" alt="no image" /></a>';
            }
            echo '
          </div><!-- end #event-arc-cover -->
          <div class="event-arc-text">
            <h2 class="event-arc-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>
              <div class="event-arc-info">';
                if($venue) {
                    echo '<p class="event-arc-venue">' . $venue . '</p>';
                }
                if (get_post_meta($post->ID, 'event_allday', true) == 'yes'){
                    echo '<p class="event-arc-time">All Day</p>';
                } elseif ($tstart) {
                    echo '<p class="event-arc-time">' . $tstart . '';
                } if ($tend) {
                    echo ' – ' . $tend . '</p>';
                }
            echo '
              </div><!-- end #event-arc-info -->';
                echo ' ' . the_excerpt_max(165) . ' ';
    
    	if (get_post_meta($post->ID, 'event_disable', true) == 'no') {
            if ($event_text) {
                echo '
                            <div class="event-tickets"><a href="' . $event_ticket . '" target="_blank">' . $event_text . '</a></div>';
            } else {
                if (get_post_meta($post->ID, 'event_out', true) == 'yes') {
                    echo '
                            <div class="event-cancel-out"><p>Sold Out</p></div>';
                } elseif (get_post_meta($post->ID, 'event_cancel', true) == 'yes') {
                    echo '
                            <div class="event-cancel-out"><p>Canceled</p></div>';
                } elseif (get_post_meta($post->ID, 'event_free', true) == 'yes') {
                    echo '
                            <div class="event-cancel-out"><p>Free Entry</p></div>';
                } else {
                    echo '
                            <div class="event-tickets"><a href="' . $event_ticket . '" target="_blank">Buy Tickets</a></div>';
                }
            }
    	}
    
            echo '
          </div><!-- end #event-arc-text -->
        </div><!-- end #event-archive -->';	
    
    	endwhile;
    else:
    echo '<h4>Sorry, no past events.</h4>';
    endif;
    ?>
    
      </div><!-- end .col-blog-archive -->
    </div><!-- end .fixed-->  
    
    <?php get_footer(); ?>

    THESE ARE THE FUNCTIONS FOR THE EVENT:

    `//Events

    add_action( ‘init’, ‘events_mt’ );
    add_action(‘init’, ‘event_taxonomies’, 0);

    function events_mt() {
    register_post_type( ‘events’,
    array(
    ‘labels’ => array(
    ‘name’ => __( ‘Events’, ‘post type general name’, ‘applause’ ),
    ‘singular_name’ => __( ‘Events Item’, ‘post type singular name’, ‘applause’ ),
    ‘add_new’ => __( ‘Add New Event’,’applause’ ),
    ‘add_new_item’ => __( ‘Add New Event’, ‘applause’ ),
    ‘edit_item’ => __( ‘Edit Event’, ‘applause’ ),
    ‘new_item’ => __( ‘New Event’, ‘applause’ ),
    ‘view_item’ => __( ‘View Event’, ‘applause’ ),
    ‘search_items’ => __( ‘Search Events’, ‘applause’ ),
    ‘not_found’ => __( ‘No Events found’, ‘applause’ ),
    ‘not_found_in_trash’ => __( ‘No Events found in Trash’, ‘applause’ ),
    ‘parent_item_colon’ => ”
    ),

    ‘has_archive’ => true,
    ‘public’ => true,
    ‘publicly_queryable’ => true,
    ‘show_ui’ => true,
    ‘query_var’ => true,
    ‘rewrite’ => true,
    ‘capability_type’ => ‘post’,
    ‘hierarchical’ => false,
    ‘menu_position’ => null,
    ‘exclude_from_search’ => true,
    ‘supports’ => array(‘title’,’editor’,’thumbnail’),
    ‘menu_icon’ => get_template_directory_uri(). ‘/img/icon-events.png’
    )
    );
    }

    function event_taxonomies() {
    register_taxonomy(‘events’, ‘event’, array(
    ‘hierarchical’ => true,
    ‘slug’ => ‘events’,
    ‘label’ => __(‘Category’, ‘applause’),
    ‘query_var’ => true,
    ‘rewrite’ => true
    ));
    }

    Am I missing anything?

  • The topic ‘PHP – Query for custom post type not displaying // creating theme event list’ is closed to new replies.