Support

Account

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

  • Hi Elliot,

    In the documentation, it says to call the $date as such:

    echo $date->format('d-m-Y');

    When I use this to call the $date, I get a “Fatal error: Call to a member function format() on a non-object…”

    Here’s what I have

    <?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',
    	);
    	
    
    	$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>';
    			
    	/* 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 '<p>' . $date->format('F n Y') . '</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(); 
    ?>