Home › Forums › Calendar Products › Events Calendar PRO › Calendar and widget meta query modification
- This topic has 7 replies, 3 voices, and was last updated 8 years, 4 months ago by
Cliff.
-
AuthorPosts
-
December 11, 2017 at 12:20 am #1402622
Edgars
ParticipantHello.
I have made a pre_get_posts function, which sets up meta query, for excluding events from /events page and widget.
Field is made with acf plugin.
But there is one problem. On /events page function is working fine, but on widget it is only working corectly then, when on a day is only one event. If on a day is multiple events it still showing “hidden” event. If i set to not show all events on a day, the also function is working like it should be. Hop this is fixable trought my code.So here is my code :
add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' ); function tribe_exclude_events_category_month_list( $query ) { if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) || isset( $query->query_vars['tribe_render_context'] ) && $query->query_vars['tribe_render_context'] == 'widget' ) { if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) { $ids = get_posts(array( 'fields' => 'ids', // Only get post IDs 'posts_per_page' => -1, 'post_type' => 'tribe_events' )); foreach ($ids as $id) { delete_post_meta($id, 'show_in_main_calendar', ' '); } $query->set( 'meta_query', array( array( array( 'key' => 'show_in_main_calendar', 'compare' => 'NOT EXISTS', ), ), )); } } return $query; }It is modificated from this post: https://theeventscalendar.com/support/forums/topic/widget-taxonomy-filter-conflict/
Support provider code in that post:
/* * The Events Calendar Remove Events from Month and List Views * add coding to theme's functions.php * @version 3.12 * modify here with event category slugs: array( 'concert', 'convention' ) */ add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' ); function tribe_exclude_events_category_month_list( $query ) { if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) { if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) { if ( isset( $query->query_vars['tribe_render_context'] ) && $query->query_vars['tribe_render_context'] == 'widget' ) { return; } $query->set( 'tax_query', array( array( 'taxonomy' => Tribe__Events__Main::TAXONOMY, 'field' => 'slug', 'terms' => array( 'musical-feasts' ), 'operator' => 'NOT IN' ) ) ); } } return $query; }December 12, 2017 at 11:50 pm #1404350Edgars
ParticipantHello.
Is there somenone who could try to help me? :/December 13, 2017 at 8:07 pm #1405162Cliff
MemberHi, Edgars.
Sorry for the delayed reply. I’m unsure exactly how that happened, but I’ll be sure to stick with you on this one (as best I can within our Scope of Support / Terms).
While I can’t write fully custom code for you, I can point out a few things I noticed:
- That code is for an outdated version of our plugins; I can tell from the @version 3.12 part of that code.
- You should instead use the tribe_events_pre_get_posts action hook. This would allow you to simplify some of your code because it would make some of it unnecessary.
- Similarly, you should use tribe_get_events (https://theeventscalendar.com/knowledgebase/using-tribe_get_events/) instead of get_posts
- Finally, I noticed a show_in_main_calendar meta key is used. If the field exists and is equal to a single space, the key gets deleted, then the query finds events without this meta key. I’m pointing this out just to make sure this is designed as you want it to be.
Please let me know if I got all that correct and how I might be able to help further.
December 18, 2017 at 3:45 am #1407667Edgars
ParticipantHello.
First, sorry for late response.tribe_events_pre_get_posts action hook is hiding all events from calendar for me. Is there some guidlines for hiding events on calendar and/or widgets based on some meta value?
$query->set( 'meta_query', array( array( array( 'key' => 'show_in_main_calendar', 'compare' => 'NOT EXISTS', ), ), ));This set query is working fine on main calendar. Widget calendar has one problem. This set query is working only then when there is only one event on a day, but if there is more than one, it is not working.
Another situation when on wdget this set is woirking is then if all of single day events are hided by this meta value.
So yeah, is there some guidlines for hiding events based on meta values?
Hope i told my situation understandable.December 18, 2017 at 12:59 pm #1408171Cliff
MemberWP_Query arguments should work as expected.
It looks like you have one too many levels of array()
Once you get something working with pre_get_posts, I’d recommend swapping it out in favor of tribe_events_pre_get_posts
December 19, 2017 at 1:44 am #1408548Edgars
ParticipantHi.
Thanks for tips. Managed to update my code and fix my problem.
The main problem solver was extra if statements, which checked ‘eventsDisplay’ query var.Thanks for help. 😉
December 19, 2017 at 6:57 pm #1409337Cliff
MemberI’m very glad you got it figured out. Have a great rest of your week.
-
AuthorPosts
- The topic ‘Calendar and widget meta query modification’ is closed to new replies.
