Support

Account

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

Solved

Filtering CPT with date picker custom field

  • I’ve been beating my head on my desk for 1-1/2 days now trying to figure out how to display a custom post type (“Event”) and sorting it by custom field date picker. I feel like I’ve tried everything although my php skills are lacking.

    Basically, I have a custom post type, “Event”, and I want my template to display the CPT and order it by date. So, it would show:

    Event title – <?php the_title(); ?>
    Date: (date-picker) – <?php the_field(‘class_date’); ?>
    Time: (text field) – <?php the_field(‘time’); ?>
    Except – <?php the_excerpt(); ?>

    I’ve tried several things and I either get php errors or nothing at all. Currently my code (that shows nothing at all) is:

    function custom_loop() {
    global $paged;
    $args = array('post_type' => 'event','posts_per_page' => '-1','meta_key' => 'class_date', 'orderby' => 'meta_value_num','order' => 'ASC',);
    query_posts ($args);
    ?>
     
     
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
                
                <div class="item">
    				<h4><?php the_title(); ?></h4>
                    <p>Date: <?php the_field('class_date'); ?></p>
     
    <?php
    /*
    *  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('date_picker'));
    echo $date->format('ddmmyy');
     
    /*
    *  Order Posts based on Date Picker value
    *  this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
    */
     
    $posts = get_posts(array(
    	'meta_key' => 'custom_order', // name of custom field
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC'
    ));
     
    if( $posts )
    {
    	foreach( $posts as $post )
    	{
    		setup_postdata( $post );
     
    	}
     
    	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    }
     
    ?>
                     <p>Time: <?php the_field('time'); ?></p>
    				<?php the_excerpt(); ?>
    				
    			</div>
    			
    <?php endwhile; ?>
    <?php endif; ?>
  • Hi @blueturtlefl

    Thanks for including your code. Have you debugged it yet? Do you knwo at which line the issue is at?

    I noticed that the function ‘custom_loop’ is never called. Is this correct?

    Also, you use many different field_names, which I think is incorrect. Perhaps you could use the search at the top of the page, to search through the many topics that talk about ordering posts via a date picker value?

    Thanks
    E

  • 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!!!

  • Hi @blueturtlefl

    Looking at your code, I can see that you are running the $dat formatting code BEFORE the actual loop. I think you need to move this inside the loop so it runs for each post?

    Thanks
    E

  • Hi Elliot,

    As you suggested, I’moved the $date formatting to be within the loop. I’m still pulling the wrong date for each post. For some reason it seems to be pulling the wrong date for each post. I have no idea where it’s getting this date from since none of the posts have this date. Below is my code and live link:

    http://www.mimismanners.blueturtlegraphics.com/classes/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',
    	);
    	
    
    	$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('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(); 
    ?>
  • Hi @blueturtlefl

    I can see in your code that you load the ACF date picker value into a variable called $date, but then you never use it to output the value…

    Perhaps you could re-read the documentation to see how you format the datetime object?

    Thanks
    E

  • 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(); 
    ?>
  • Hi @blueturtlefl

    before you use the format function on the $date variable, can you please debug it’s value like so:

    
    <?php 
    
    echo '<pre>';
    	print_r( $date );
    echo '</pre>';
    die; ?>
    

    Also, can you please debug the value of your date field like so:

    
    <?php 
    
    echo '<pre>';
    	print_r( get_field('class_date') );
    echo '</pre>';
    die; ?>
    

    It is most likely that the date value is not in the correct format to be converted into the date-time object as your code is expecting. This debugging will highlight the issue

    Thanks
    E

  • Hi Elliot,

    How exactly should I use this debugging code? Sorry, PHP newbie, here.

    Kim

  • Hi Elliot,

    I was able to figure out how to debug the value. When I added the 2nd set of debug code, it pulled the correct date (20131129) but it’s not formatting correctly.

    I tried adding the $date variable debug code and got a blank white screen. Not sure if I’m doing this correctly. It seems it’s having an issue with the formatting of the date.

    Any ideas?

  • Hi Elliot,

    I think I found my solution. I found it at the bottom of this thread:
    http://support.advancedcustomfields.com/forums/topic/datepicker-wrong-format-is-displaying/

    I changed my code as follows:

    $date = DateTime::createFromFormat('Ymd', get_field('class_date'));
    echo '<p>' . $date->format('F j, Y') . '</p>';

    I changed ‘yymmdd’ to ‘Ymd’ and then changed the format value from ‘F n Y’ to ‘F j, Y’.

    I’m now confused because the documentation tells us to use the save format yymmdd and since we are using the Jquery picker, to format using the JS Date Format. I never thought to try PHP Date format and it worked. Maybe I don’t fully understand the documentation, but this is where some of my confusion lies. Anyway, the code seems to be pulling the correct date and formatting the way I configured it to.

    Thank you for your help!
    Kim

  • Hi @blueturtlefl

    Thanks for the follow up. Yes, I must have missed that you were using an incorrect PHP date format string.

    For some unknown reason, JS and PHP use different format strings. So in JS, the string is yymmdd, but in PHP, the string is Ymd

    Thanks
    E

Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘Filtering CPT with date picker custom field’ is closed to new replies.