Adding a custom taxonomy or event meta filter

Home Forums Calendar Products Events Calendar PRO Adding a custom taxonomy or event meta filter

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1145573
    Sunny Bains
    Participant

    We use a quite complicated taxonomy system which we are using with our events, and I have also added a number of custom meta items to them which I have added filters for using the action hook ‘pre_get_posts’

    When loading the events week or month view from a direct link or refreshing the filters apply, however if I scroll through months from the navigation links at the bottom of the month view they do not (until I refresh).

    Here’s a snippet of the code I’m using for a simple meta filter:

    add_action( 'pre_get_posts', 'get_tribe_events_posts_exclude_cancelled' );
    function get_tribe_events_posts_exclude_cancelled( $query ) {
    	if ( is_post_type_archive( 'tribe_events' ) &&  !is_admin() ) {
    		$metaquery[] = array(
    			array(
    				'key' 	   => get_event_custom_field_key('Event Cancelled'),
    				'value'    => 'Yes',
    				'compare'  => '!=',
    			),
    		);
    		$query->set('meta_query', $metaquery);
    	}
    }

    Note get_event_custom_field_key is a custom function I created to find the postmeta ket from the name used when creating it, (is there a proper function for this?)

    Also, is it possible when searching or browsing through the function arachive to be able to hide depricated functions? As a new user I don’t want to use them and there are a lot of them.

    #1146014
    Sunny Bains
    Participant

    Looking a little more into this is seems to be specificly related to the ‘Next/Previous Week/Month’ links.

    If I access a calendar page by linking to the URL directly, or typing it, or by switching between the week/month views then it works fine. If get to the page using the next/previous links then the events that were excluded by ‘pre_get_posts’ are still visible UNTIL I refresh the page. Also use of the browsers back button causes the filtered events to still be visible.

    If you want to see this happening I can share HTTP access details for the developemnt site to you privately.

    #1146285
    Cliff
    Member

    Hi Sunny. Thanks for your detailed question.

    I believe I understand what you’re asking for, but we’re limited in helping with customizations, per our Scope of Support / Terms. However, I can try to help point you in the right direction.

    I’m not certain if what you’re experiencing is what is to be expected, but I can tell you that the List View’s prev/next links come from https://github.com/moderntribe/the-events-calendar/blob/4.2.3/src/views/list/nav.php

    which runs tribe_get_listview_dir_link() from https://github.com/moderntribe/the-events-calendar/blob/4.2.3/src/functions/template-tags/link.php#L168

    which has a filter.

    So maybe you could leverage the tribe_get_listview_dir_link hook to add a query string parameter to all prev/next URLs (assuming your custom taxonomy is available via a query string). Or maybe reviewing that function will point you in another direction that may be a better type of solution.

    Please let me know if you get any further along after reviewing this information.

    #1146482
    Sunny Bains
    Participant

    Digging deeper into this, the reason it works sometimes and not if I use the next/previous links is the function ‘pre-get_posts’ in /the-events-calendar/src/Tribe/Query.php overwrites my meta_query rather than adding to it.

    If I move assigning the query from functions.php to /the-events-calendar/src/Tribe/Query.php all is well.

    The code I have added to Query.php at line 347 (after the block commeted as ‘hide upcoming events from query (only not in admin)’):

    if ( !Tribe__Admin__Helpers::instance()->is_screen( 'edit-tribe_events' ) ) {
    	// Filter out any cancelled events
    	$meta_query[] = array(
    		'key' 	   => get_event_custom_field_key('Event Cancelled'),
    		'value'    => 'Yes',
    		'compare'  => '!=',
    	);
    }

    since this is a core plugin file is there any way I can override this so that upgrades will still work without breaking the changes?

    • This reply was modified 7 years, 9 months ago by Sunny Bains.
    #1146811
    Cliff
    Member

    Sunny, please switch from using pre_get_posts to tribe_events_pre_get_posts instead (or you might need to hook that single function into both) and see if that works for you.

    #1146946
    Sunny Bains
    Participant

    I had reviously tried hooking one tribe_events_pre_get_posts which didn’t work. I have also now tried hooking to both but still it doesn’t work.

    Is there an easy way for me to override function pre_get_posts in /the-events-calendar/src/Tribe/Query.php?

    #1147170
    Cliff
    Member

    Thanks for confirming that.

    After consulting with one of our developers, this is something I’ve logged as a potential bug to investigate further but we don’t have any fix available for your situation at this time. Sorry about that.

    You might be able to unhook our pre_get_posts() method (from pre_get_posts action priority 50) and hook in your replacement, for which would you’d have to copy the existing functionality and then bake in your own tweaks.

    I hope that is helpful information to you.

    #1147564
    Sunny Bains
    Participant

    I haven’t been able to try this today and I’m now away for a week. Can we keep this open so I can pick it up when I get back.

    I would like to know, when using remove action what global variable is assigned to the class Tribe__Events__Query?

    #1147937
    Cliff
    Member

    Alrighty, sir. Let’s try this new feedback from our developers…

    This code should work for you:

    add_action( 'pre_get_posts', 'get_tribe_events_posts_exclude_cancelled' );
    function get_tribe_events_posts_exclude_cancelled( $query ) {
    if (
    ( is_post_type_archive( 'tribe_events' ) && ! is_admin() )
    || (
    defined( 'DOING_AJAX' )
    && DOING_AJAX
    && ! empty( $query->query['tribe_render_context'] )
    && 'default' === $query->query['tribe_render_context']
    && ! empty( $query->query['post_type'] )
    && Tribe__Events__Main::POSTTYPE === $query->query['post_type']
    )
    ) {
    $metaquery[] = array(
    array(
    'key' => get_event_custom_field_key('Event Cancelled'),
    'value' => 'Yes',
    'compare' => '!=',
    ),
    );
    $query->set('meta_query', $metaquery);
    }
    }

    Notes:

    • Hooking to pre_get_posts is likely the right plan
    • Our Query object does not override the meta_query
    • Pagination happens via Ajax
    • Ajax requests are admin pages so your ! is_admin() is preventing the meta query from being manipulated during pagination
    • The tribe_render_context check ensures the query only manipulates the main loop and not widgets

    I hope this works swimmingly for you! Please let me know.

    #1156439
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Adding a custom taxonomy or event meta filter’ is closed to new replies.