WordPress.org

Forums

CPT with ACF custom date fields -- few things (4 posts)

  1. bl1nktank
    Member
    Posted 1 year ago #

    I'm having a few difficulties with my Event CPT...

    1. Sorting the rows in admin view by start_date (ACF)
    2. Filtering get_posts to show upcoming shows only

    For now, here's my code for the template:

    <?php
    
    							$today = date('Ymd');
    							echo $today;
    							settype($today, "string");
    
    							// Get upcoming boat shows
    							$boatshows = get_posts( array(
    								'post_type' => 'event',
    								'meta_query' => array(
    									'key' => 'end_date',
    									'compare' => '<=',
    									'value' => $today ),
    								'meta_key' => 'start_date',
    								'order_by' => 'meta_value_num',
    								'order' => 'DESC',
    								'posts_per_page' => 15
    							));
    
    							if ( $boatshows ) {
    								echo '<div style="text-align: center;">';
    								foreach ( $boatshows as $boatshow ) {
    									setup_postdata( $boatshow );
    
    									echo '<p><a href="' . get_permalink($boatshow->ID) . '"><strong>' . apply_filters( 'post_title', $boatshow->post_title ) . '</strong> (';
    									echo get_post_meta($boatshow->ID, 'start_date', true);
    									$startd = DateTime::createFromFormat('Ymd', get_post_meta( $boatshow->ID, 'start_date', 1 ));
    									$endd = DateTime::createFromFormat('Ymd', get_post_meta ($boatshow->ID, 'end_date', 1 ));
    									echo $startd->format('F d') . ' &ndash; ' . $endd->format('F d');
    									echo ')</a></p>';
    								}
    								echo '</div>';
    
    								wp_reset_postdata();
    							}
    
    							?>

    You see there's some stuff in there just to see what results I'm getting...

    But if I change the 'order' key in get_posts, nothing changes. Also I can't get the meta_query filter to work.

    Please help! Thank you.

  2. bl1nktank
    Member
    Posted 1 year ago #

    Re-posting the code sans extra tabs:

    <?php
    
    $today = date('Ymd');
    echo $today;
    settype($today, "string");
    
    // Get upcoming boat shows
    $boatshows = get_posts( array(
    	'post_type' => 'event',
    	'meta_query' => array(
    		'key' => 'end_date',
    		'compare' => '<=',
    		'value' => $today ),
    	'meta_key' => 'start_date',
    	'order_by' => 'meta_value_num',
    	'order' => 'DESC',
    	'posts_per_page' => 15
    ));
    
    if ( $boatshows ) {
    	echo '<div style="text-align: center;">';
    	foreach ( $boatshows as $boatshow ) {
    		setup_postdata( $boatshow );
    
    		echo '<p><a href="' . get_permalink($boatshow->ID) . '"><strong>' . apply_filters( 'post_title', $boatshow->post_title ) . '</strong> (';
    		echo get_post_meta($boatshow->ID, 'start_date', true);
    		$startd = DateTime::createFromFormat('Ymd', get_post_meta( $boatshow->ID, 'start_date', 1 ));
    		$endd = DateTime::createFromFormat('Ymd', get_post_meta ($boatshow->ID, 'end_date', 1 ));
    		echo $startd->format('F d') . ' &ndash; ' . $endd->format('F d');
    		echo ')</a></p>';
    	}
    	echo '</div>';
    
    	wp_reset_postdata();
    }
    
    ?>
  3. bl1nktank
    Member
    Posted 1 year ago #

    And to clarify ACF = Advanced Custom Fields. Sorry about that.

  4. bl1nktank
    Member
    Posted 1 year ago #

    Never mind I got it

Topic Closed

This topic has been closed to new replies.

About this Topic