• I have a custom coded events post type where the page is working just fine with the exception of not hiding past events. I need the page to auto-hide events as the “start date” is passed

    Here is the code for the page:

    <?php get_header();?>
    
    <div class="page-container row-fluid">
    	<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    
    			<div class="breadcrumb">
    				<div class="container">
    					<a href="<?php echo home_url();?>">Home</a> » <a href="events">Events </a>
    					<h1>Events</h1>
    				</div>
    			</div>
    
    		  <div class="container">
    			  	<div class="span8">
    
    			  		<?php 
    
    			  		$args= array(
    			  			'post_type'		=> 'events',
    						'posts_per_page'	=> -1,
    						'meta_key'		=> 'start_date',
    						'orderby'		=> 'meta_value_num',
    						'order'			=> 'ASC'
    			  		);
    
    			  		$wp_query = new WP_Query($args);
    
    			  		//if ( have_posts() ) : ?>
    
    			  		<?php while($wp_query->have_posts()) : $wp_query->the_post();?>
    
    			  		<div class="entry-content">
    			  			<div class="event-title">
    			  				<h2><a href="<?php the_permalink()?>"><?php the_title()?></a></h2>
    			  			</div>
    
    			  			<div class="meta">
    	  						<p><?php if(get_field('start_date')):?>
    
    	  						<?php $date = get_field('start_date');
    	  						// $date = 19881123 (23/11/1988)
    
    	  						// extract Y,M,D
    							$y = substr($date, 0, 4);
    							$m = substr($date, 4, 2);
    							$d = substr($date, 6, 2);
    
    	  						// create UNIX
    							$time = strtotime("{$d}-{$m}-{$y}");
    
    	  						?>
    
    	  						<strong>Date:</strong> <?php echo date('m/d/Y', $time);// format date (11/23/1988)?> <?php endif; ?><?php if(get_field('locations')):?>| <strong>Location:</strong> <?php the_field('locations')?> <?php endif; ?></p>
    	  					</div>
    
    	  					<div class="clearfix"></div>
    
    			    		<?php the_excerpt();?>
    			    		<hr>
    			    	</div>
    			  		<?php endwhile; //wp_reset_query(); ?>
    
    			  		<?php
    					// pagination
    					defaulttheme_content_nav( 'nav-below' ); ?>
    
    					<?//php else : ?>
    						<?//php get_template_part('not', 'found')?>
    					<?//php endif; // end have_posts() check ?>
    
    			  	</div>
    
    		  		<div class="sidebar span4">
    		  			<?php if ( is_active_sidebar( 'buttons_sidebar' ) ) : ?>
    				        	<?php dynamic_sidebar( 'buttons_sidebar' );  ?>
    				        <?php endif;?>
    		  			<?php if ( is_active_sidebar( 'events_sidebar' ) ) : ?>
    				        	<?php dynamic_sidebar( 'events_sidebar' );  ?>
    				        <?php endif;?>
    		        	<?php if ( is_active_sidebar( 'general_sidebar' ) ) : ?>
    		        		<?php dynamic_sidebar( 'general_sidebar' );  ?>
    		        	<?php endif;?>
    				 </div>
    
    		  </div>
    
    	  </div>
    </div>
    <?php get_footer();?>

    Can anyone help me figure out what I am doing wrong as I thought the page was already coded to hide an event when the start date passed.

    Thanks,

Viewing 4 replies - 1 through 4 (of 4 total)
  • I took a quick look at your code…

    It looks like you’re missing an if statement that checks if the event start date is before or after the current date.

    …. one sec. I’m taking a better look…

    Thread Starter anspaujd

    (@anspaujd)

    How and where would that if statement need to go? I am a newbie when it comes to php.

    One sec.. let me see…

    <?php while($wp_query->have_posts()) : $wp_query->the_post();?>
    >>>> if statement should be here…
    <div class="entry-content">

    There are quite some syntax errors in your PHP so be sure to check it.

    Use the code that starts with if(get_field('start_date'))...
    to get a date so that is formatted to generate the date as 11/13/2014
    Then start an if statement that checks if this date is greater than today’s date using the the_date('m/d/Y'); function from WP

    Then the rest

    This should point you in the right direction… be sure you get all the syntax correct or it won’t work

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hide based on date’ is closed to new replies.