removement
8/2/2019 - 4:26 PM

ACF Block Custom Field Query

How to query posts and display custom fields in an ACF gutenberg block

<?php
$events = new WP_Query( array( 
    'post_type' => 'events', 
    'orderby' => 'date', 
    'order' => 'ASC',
));
?>

<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
		<h2>Events</h2>
		<?php if ( $events->have_posts() ):
            while ( $events->have_posts() ) : $events->the_post(); ?>
                <div class="event">
                    <h5><?php the_title(); ?></h5>
                    <strong><?php echo get_field( 'event_date', get_the_ID() ); ?></strong>
                    <div><?php echo get_field( 'event_location', get_the_ID() ); ?></div>
                    <?php if( get_field( 'event_details', get_the_ID() ) == 'website' ): ?>
                        <a href="<?php echo get_field( 'event_website_link', get_the_ID() ); ?>" target="_blank">Event Details</a>
                    <?php endif; ?>
                    <?php if( get_field( 'event_details', get_the_ID() ) == 'pdf' ): ?>
                        <a href="<?php echo get_field( 'event_pdf', get_the_ID() ); ?>" target="_blank">Event Details</a>
                    <?php endif; ?>
                </div>
			<?php endwhile; ?>
		<?php wp_reset_postdata();
		endif; // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
		<p><a href="https://www.mypcnow.org/resources/submit-an-event/" class="btn btn-primary">Submit an event</a></p>
</div>