Remove and add action to override addQueryArgs for FilterBar

Home Forums Calendar Products Filter Bar Remove and add action to override addQueryArgs for FilterBar

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #974867
    gonzalosanza
    Participant

    Hello team!
    I need to override the function addQueryArgs around line 109 on the-events-calendar-filter-bar/src/Tribe/Filter.php
    so, I thought to remove the action on same file, line 88.

    It means, paste this code on my functions.php

    remove_action( ‘tribe_events_pre_get_posts’, array( $this, ‘addQueryArgs’ ), 10 );
    add_action( ‘tribe_events_pre_get_posts’, array( $this, ‘addQueryArgsCustom’ ), 10 );

    function addQueryArgsCustom( $query ) {

    if ( ! $query->tribe_is_event ) {
    // don’t add the query args to other queries besides events queries
    return;
    }

    if ( isset( $_REQUEST[‘tribe_’ . $this->slug] ) && $_REQUEST[‘tribe_’ . $this->slug] != ” ) {
    if ( !empty( $this->joinClause ) ) {
    add_filter( ‘posts_join’, array( $this, ‘addQueryJoin’ ), 11, 2 );
    }
    if ( !empty( $this->whereClause ) ) {
    add_filter( ‘posts_where’, array( $this, ‘addQueryWhere’ ), 11, 2 );
    }
    foreach ( $this->queryArgs as $key => $value ) {
    $query->set($key, $value);
    }
    }
    }

    But, it’s not working, probably because I must to use somthing instead $this. I tried with Tribe__Events__Filterbar__Filter::instance() but still nothing.

    Any little help please?
    Thanks!

    #975175
    Nico
    Member

    Howdy Gonzalo,

    Thanks for reaching out! Interesting question here, I’ll try to help you out.

    When you remove the action, the first value in the array should be a string with class name and the second one point to the method in that function. Now, when you add your own action, as you are using a custom function that’s not inside the class you should just input the function name there.


    remove_action( 'tribe_events_pre_get_posts', array( 'Tribe__Events__Filterbar__Filter', 'addQueryArgs' ), 10 );
    add_action( 'tribe_events_pre_get_posts', 'addQueryArgsCustom', 10 );

    function addQueryArgsCustom( $query ) {
    //your code
    }

    Please give this a try and let me know how it goes,
    Best,
    Nico

    #976145
    gonzalosanza
    Participant

    Really thanks Nico! your code seems to work! but im still having problem using $this inside the function addQueryArgsCustom. I also tried with Tribe__Events__Filterbar__Filter::instance() but doesn’t works.
    Any idea? Thanks!

    #984574
    Nico
    Member

    Gonzalo,

    Sorry to say I wasn’t looking at the code inside your function, lazy me 🙁

    I get what you are trying to do there, tried a couple of things to get the instance there but had no luck with that. I’ll ping someone in the dev team to give me a hand on this. I’ll get back to you as soon as I have a answer for this.

    Best,
    Nico

    #984741
    Nico
    Member

    Hey Gonza,

    It would be great to know what you are trying to achieve there! The custom function you are using to replace ours with is actually identical to ours. Knowing this might help us find a way to solve this. Solution might not be as straightforward as unhooking and re-hooking that action.

    Thanks for your patience on this,
    Nico

    #984792
    gonzalosanza
    Participant

    Thanks Nico! It make sense!
    I have some custom filters on filter bar, based on the filter category. To make theme work propperly, I had to override the addQueryArgs function to this one (It’s very similar to the original, thats way I paste the original on the bigining, sorry):

    public function addQueryArgs( $query ) {
    
    			if ( ! $query->tribe_is_event ) {
    				// don't add the query args to other queries besides events queries
    				return;
    			}
    
    			if ( isset( $_REQUEST['tribe_' . $this->slug] ) && $_REQUEST['tribe_' . $this->slug] != '' ) {
    				if ( !empty( $this->joinClause ) ) {
    					add_filter( 'posts_join', array( $this, 'addQueryJoin' ), 11, 2 );
    				}
    				if ( !empty( $this->whereClause ) ) {
    					add_filter( 'posts_where', array( $this, 'addQueryWhere' ), 11, 2 );
    				}
    				foreach ( $this->queryArgs as $key => $value ) {
    					if($key=='tax_query'){
    						$taxquery=$query->get('tax_query');
    						if($taxquery!=null){
    							foreach($value as $v){
    								array_push($taxquery,$v);
    							}
    						}else{
    							$taxquery=$value;
    						}
    					}
    					$query->set($key, $taxquery);
    				}
    			}
    		}

    It works, but If I update the plugin… Thats way I would like to unhook, rehook if its possible.

    Really thanks for your time!!

    • This reply was modified 8 years, 10 months ago by gonzalosanza.
    #985135
    Nico
    Member

    Gonza,

    Thanks for the follow-up! Nice piece of code I have to say.

    I’ve check your code with the dev team, and the thing is there is no better -‘more sustainable’- way of doing that at the time 🙁
    The good thing is that per dev suggestion I added this as a feature request and a filter to achieve this will be -probably- included in our code. Not sure if it will make it to upcoming release, but I’ll try to push it forward.

    Please be sure to check release note for upcoming version of FilterBar, to see if this is included. I’ll try to have this on my radar and let you know as well.

    At this point I’m suspecting you speak Spanish, please let me know if you do.

    Best,
    Nico

    #986915
    gonzalosanza
    Participant

    OK Nico, thanks anyway for your help!
    Yes! I speak spanish!
    Regards!

    #986977
    Nico
    Member

    Buenísimo Gonza, voy a estar pendiente a este cambio entonces. Cuando este implementado te aviso por e-mail (a la dirección que figura en tu usuario).

    Te parece bien si cierro esta conversación Si surge alguna duda relacionada siempre podes abrir una nueva y linkear a esta como referencia.

    Lamento que no haya otra forma de resolverlo por ahora, gracias por la paciencia!

    Saludos,
    Nico

    #986980
    gonzalosanza
    Participant

    OK, ningún problema, cierra la conversación. Como dices, agradecería si me notificas por email si hay alguna novedad.
    Gracias!
    Un saludo!

    #987498
    Nico
    Member

    Gonza,

    Me comprometo a hacerlo!

    Saludos,
    Buen finde,
    Nico

    #988114
    Leah
    Member

    Hi there,

    We wanted to check in here and update you on our progress. Although we weren’t able to address your issue in our upcoming release (3.11) we do still have it on our radar. Thank you for your patience while we continue working on a fix for the problem you reported. We will do our best to keep you posted when we have a solution in place. In the meantime, if you need help or find that the issue you reported here is no longer a problem, please feel free to start another thread.

    Thanks again for your patience and understanding!

    Cheers,
    Leah
    and the rest of The Events Calendar team

Viewing 12 posts - 1 through 12 (of 12 total)
  • The topic ‘Remove and add action to override addQueryArgs for FilterBar’ is closed to new replies.