Skip to content

Instantly share code, notes, and snippets.

@jasperf
Last active November 1, 2021 03:06
Show Gist options
  • Save jasperf/7a3ff820bdf89b73fb92aaf1c7e9b8a4 to your computer and use it in GitHub Desktop.
Save jasperf/7a3ff820bdf89b73fb92aaf1c7e9b8a4 to your computer and use it in GitHub Desktop.
Loading Events CPT Posts as ACF Block
<?php
/**
* Events CPT Block
*
**/
// Create id attribute allowing for custom "anchor" value.
$id = 'pm-' . $block['id'];
if( !empty($block['anchor']) ) {
$id = $block['anchor'];
}
// Create class attribute allowing for custom "className" and "align" values.
$className = 'block-pm';
if( !empty($block['className']) ) {
$className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
$className .= ' align' . $block['align'];
}
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$terms = get_field('events_category');
$term_slugs ='';
foreach ( $terms as $term ):
$term_slugs .= "'" . esc_html( $term->slug ) . "'" . ',';
endforeach;
$term_slugs = substr($term_slugs, 0, -1); //Removes very last comma.
// echo $term_slugs;
// local,virtual,
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
// 'event_category' => $term_slugs
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => array($term_slugs),
],
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment