Events is taxonomy 'post_type' => 'events' but when I add
$term = get_queried_object()->term_id;
print_r($term);
to code here inside the while loop nothing is printed. The new query does already load the events custom post type, but now with fields added I want to load them too.
<?php
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_categories = get_field('event_categories');
$event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => $number_of_events,
'order' => 'DESC',
);
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'event_category',
'field' => 'term_id',
'terms' => $event_cat_ids
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) : ?>
<div class="event-grid">
<?php while( $query->have_posts() ): $query->the_post();
$term = get_queried_object()->term_id;
print_r($term);
// $date_and_time = get_field('date_and_time', $term);
// print_r($date_and_time);
// $date_number = get_field('date_number');
// $date_number = get_field('date_and_time');
// $event_title = get_field('event_title');
// $event_location = get_field('event_location');
// $event_description = get_field('event_description');
?>
<div class="block-box event-box">
<div class="event-number-container">
<p class="event-number">date<?php //echo $date_number; ?>
</div>
<div class="event-details">
<p class="event-date-time"><?php // echo $date_and_time; ?>
<h4 class="event-title"><?php the_title(); ?></h4>
<p class="event-location">event location<?php // echo $event_location; ?>
</div>
</div>
<?php endwhile; wp_reset_postdata();
endif;
?>
So I used $term = $query->get_queried_object()->term_id; and did get id 146. so making progress here.
Getting the date_and_time field for posts queried do not load yet though.. we also use more categories for the events cpt of course..
$term = $query->get_queried_object()->term_id;
// print_r($term);
$date_and_time = get_field('date_and_time', $term);
print_r($date_and_time);
I guess I may need to focus on the custom post type and not the term here?
It should be F j, Y \@ g:i a with @ escaped using a \
I have had these errors as well the last couple of days. Sometimes I could not add posts… and when I tried again was told I had already submitted the content. But I persevered…
I replaced the local database with the latest one to be sure I had no leftover blocks experimenting with all. Still I see
TypeError: null is not an object (evaluating 'e.addEventListener')
Global Code — toggle.js:7
Selected Element
<div class="block-editor-block-list__layout is-root-container" data-is-drop-zone="true" data-select2-id="3">…</div>
so perhaps it is caused by another block. Annoying though as it kills display of my new block in the editor. Seems to be related to a WordPress default block as the class is called block-editor-block-list__layout. Will try to see what it is.
Checking console in Firefox Developer I now see
Uncaught TypeError: can't access property "addEventListener", e is null
<anonymous> https://site.test/wp-content/themes/theme/blocks/pricing-toggle/toggle.js?ver=5.8.1:7
So will see if that block error is causing my block not to display well in the editor.
Hmm
<?php
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_categories = get_field('event_categories');
$event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
#$event_cat_ids = array( 1, 31 );
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => $number_of_events,
'order' => 'DESC',
);
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'event_category',
'field' => 'term_id',
'terms' => $event_cat_ids
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata();
endif;
?>?>
does work now. Virtual 3 shows me
Event 4 Virtual Event 3 Virtual Event One Virtual
Only display of block on page in editor is hidden all the time on save. Perhaps related to
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at toggle.js?ver=5.8.1:7
Not sure yet.
Hmm
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_categories = get_field('event_categories');
$event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
#$event_cat_ids = array( 1, 31 );
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => $number_of_events,
'order' => 'DESC',
);
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'event_category',
'field' => 'term_id',
'terms' => $event_cat_ids
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata();
endif;
?>
does work now. Virtual 3 shows me `Event 4 Virtual Event 3 Virtual Event One Virtual
` Only display of block on page in editor is hidden all the time on save. Perhaps related to
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at toggle.js?ver=5.8.1:7
$event_categories = get_field('event_categories');
var_dump($event_categories);
gives us
array(2) { [0]=> object(WP_Term)#4557 (11) { [“term_id”]=> int(179) [“name”]=> string(7) “Virtual” [“slug”]=> string(7) “virtual” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(179) [“taxonomy”]=> string(14) “event_category” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(3) [“filter”]=> string(3) “raw” [“meta”]=> array(0) { } } [1]=> object(WP_Term)#4445 (11) { [“term_id”]=> int(180) [“name”]=> string(5) “Local” [“slug”]=> string(5) “local” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(180) [“taxonomy”]=> string(14) “event_category” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(1) [“filter”]=> string(3) “raw” [“meta”]=> array(0) { } } }
and
array(1) { [0]=> object(WP_Term)#4581 (11) { [“term_id”]=> int(180) [“name”]=> string(5) “Local” [“slug”]=> string(5) “local” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(180) [“taxonomy”]=> string(14) “event_category” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(1) [“filter”]=> string(3) “raw” [“meta”]=> array(0) { } } }
with two categories chosen and three for number of events.
So as these are two arrays with arrays we get
NULL
Warning: foreach() argument must be of type array|object, null given in /Users/jasper/code/site/wp-includes/class-wp-list-util.php on line 164
@jarvis Thanks for that. Did add the option to choose number of posts as well so adjusted the code and am now using:
/ Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_categories = get_field('event_categories');
$event_cat_ids = wp_list_pluck( $event_categories, 'term_id' );
#$event_cat_ids = array( 1, 31 );
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => $number_of_events,
'order' => 'DESC',
);
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'event_category',
'field' => 'term_id',
'terms' => $event_cat_ids
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata();
endif;
?>
I see two times the one and only post for event category local.
This when I choose 1 event and category local. So somehow things repeat themselves. Two, I see a warning
Warning: foreach() argument must be of type array|object, null given in /Users/jasper/code/site/wp-includes/class-wp-list-util.php on line 164
and when I print print_r($event_cat_ids); I see
Array ( [0] => 180 )
Event Two Local
Array ( [0] => 180 )
Event Two Local
so closer, but not quite.
Saw something funny happening to the quotes so used decimal code for quotes:
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$terms = get_field('events_category');
// print_r($terms);
// $term_slugs = $terms()->slug;
$term_slugs ='';
foreach ( $terms as $term ):
$term_slugs .= "'" . esc_html( $term->slug ) . "'" . ',';
endforeach;
$term_slugs = substr($term_slugs, 0, -1); //Removes very last comma.
// print_r($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_values($term_slugs),
],
);
still no filtering on event category..
I think this is closer now:
// 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),
],
);
but if I added term names I need to quote them as well still. So testing
// 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),
],
);
Seems though when I use local as category which has only one post and select 4 as the number of posts I still see all four.
Do now have this
// 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;
echo $term_slugs;
which almost works for loading this is terms => array(terms):
$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),
],
);
but I need a comma after each term except for the last one
Reading https://www.advancedcustomfields.com/resources/taxonomy/#template-usage via one of the threads you helped with @hube2 I realized I can of course loop through the array:
<?php
$terms = get_field('events_category');
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo esc_html( $term->slug ); ?>
<?php endforeach; ?>
<?php endif; ?>
and then I do get local virtual .Just not with commas to use in wp_query terms yet. Will need some formatting to be used as variable in
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => $event_cat_slugs,
That and I wonder if there is a quicker way to get these for my variable. A foreach loop seems a bit much..
When I switch to term id for the field and use
$event_cats_ids = get_field('events_category');
var_dump($event_cats_ids);
I get this array
array(2) { [0]=> int(179) [1]=> int(180) }
So the situation is similar. I get an array. Using that in
'terms' => $event_cats_ids,
does not work. When I choose one local event I get the last virtual event. So I think this does not work.I need array(id, id), based on variable input somehow.
I was yesterday recommend to use something similar:
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => block_value('event-category')->name,
],
So to work with ACF fields I would then use
<?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);
$event_object = get_field_object('events_category');
// print_r($event_object);
$event_cat_name = $event_object['value'];
$event_cat_slug = $event_cat_name[0]->slug;
// print_r($event_cat_slug);
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
// 'event_category' => $event_cat_slug
'tax_query' => [
'taxonomy' => 'event_category',
'include_children' => false,
'field' => 'name',
'terms' => $event_cat_slug, //
],
);
$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; ?>
not sure yet how to get term ids loaded from the events_category field. So far I have only been able to get arrays:
print_r($event_cat_name);
Array ( [0] => WP_Term Object ( [term_id] => 179 [name] => Virtual [slug] => virtual [term_group] => 0 [term_taxonomy_id] => 179 [taxonomy] => event_category [description] => [parent] => 0 [count] => 3 [filter] => raw [meta] => Array ( ) ) [1] => WP_Term Object ( [term_id] => 180 [name] => Local [slug] => local [term_group] => 0 [term_taxonomy_id] => 180 [taxonomy] => event_category [description] => [parent] => 0 [count] => 1 [filter] => raw [meta] => Array ( ) ) )
or the first slug of slugs given by chosen event categories:
print_r($event_cat_slug);
virtual
I can switch from term object to term id. But still, how to load the term ids chosen in a variable I can add to terms in the tax_query? Any suggestions?
Reading https://support.advancedcustomfields.com/forums/topic/filtering-with-posts-that-are-using-multiple-select/ it seems I may need meta_query
When you allow multiple values the values saved is no longer a single text value. Instead it is stored in the DB as a serialized array. You need to alter the meta query
but tried a few things and not yet sure how..
// Variables
$number_of_events = get_field( 'number_of_events' );
// print_r($number_of_events);
$event_object = get_field_object('events_category');
// print_r($event_object);
$event_cat_name = $event_object['value'];
// print_r($event_cat_name);
$cat_names = array_column($event_cat_name, 'slug');
print_r($cat_names);
// $event_cat_slug = $event_cat_name[0]->slug;
// print_r($event_cat_slug);
$args = array(
'post_type' => 'event',
'posts_per_page' => $number_of_events,
'tax_query' => array(
array(
'taxonomy' => 'event_category',
'terms' => $cat_names
)
)
);
Still does not filter though $cat_names does print cats selected in multi select like when I choose onsite only: Array ( [0] => onsite )
terms should accept an array I understand but JS states
Error: Syntax error, unrecognized expression: Array
(
[0] => onsite
)
<h2>Event 4 Virtual</h2>
<h2>Event 3</h2>
<h2>Event Two</h2>
In the end to add the user role this did work
add_action( "user_register", function( $user_id ) {
$selected_radio = $_POST['field_5e44e2456a4a9'];
$user = new WP_User( $user_id );
if( $selected_radio == 'bedrijf') {
$user->set_role('bedrijf');
}
} );
Yeah, I thought it would be something like that. Thanks for sharing this @John Huebner!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.