Support

Account

Home Forums Front-end Issues Filtering CPT with date picker custom field Reply To: Filtering CPT with date picker custom field

  • Ok, I’ve changed my template code and have now managed to get the date picker and text field for time to display. My only issue now is that the dates are shown as 20131129 (yymmdd) and I want them to display as “F n Y” or November 29, 2013. I’ve tried adding in the code:

    echo date('F n Y', $time);

    but I keep getting an error:

    Fatal error: Call to a member function format() on a non-object in /home/mimismanners/mimismanners.com/wp-content/themes/dynamik-gen/my-templates/upcomingclasses.php on line 32

    HERE’S MY TEMPLATE CODE

    <?php
    /*
     * Template Name: Upcoming Classes
     */
    ?>
    
    <?php
    remove_action('genesis_loop', 'genesis_do_loop');
    add_action('genesis_loop', 'custom_loop');
    
    function custom_loop() {  
      	
    	// Intro Text (from page content)
    	echo '<div class="page hentry entry">';
    	echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
    	echo '<div class="entry-content">' . get_the_content() ;
    	
        
    	$args = array(
    		'post_type' => 'events', // enter your custom post type
    		'meta_key' => 'class_date', // name of custom field
    	    'orderby' => 'meta_value_num',
    	    'order' => 'ASC',
    	);
    	
    
    /* Create PHP DateTime object from Date Picker Value
        this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP) */
    
        $date = DateTime::createFromFormat('yymmdd', get_field('class_date'));
        echo $date->format('mmddyy');
        echo date('F n Y', $time);
    
    	$loop = new WP_Query( $args );
    	if( $loop->have_posts() ):
    	
    					
    	while( $loop->have_posts() ): $loop->the_post(); global $post;
    	
    		
    		echo '<div id="events">';
    			
    			echo '<h3>' . get_the_title() . '</h3>';
    			echo '<p>' . get_field('class_date') . '</p>';
    			echo '<p>'. get_field('time') . '</p>';
    			echo '<p>' . get_the_content() . '</p>';
            echo '</div>';
    		
    		endwhile;
    		
    	endif;
    	
    			
    }
    
    	
    /** Remove Post Info */
    remove_action('genesis_before_post_content','genesis_post_info');
    remove_action('genesis_after_post_content','genesis_post_meta');
     
    
    genesis(); 
    ?>

    Any help would me much appreciated!!!