tribe_events post type being added to custom get_posts query

Home Forums Calendar Products Events Calendar PRO tribe_events post type being added to custom get_posts query

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1121577
    pleek91
    Participant

    Hello,

    I have the following query that is being run in a pre_get_posts action.

    $args = array(
    	'post_type'			=> 'portfolio',
    	'fields'			=> 'ids',
    	'posts_per_page'	=> -1,
    	'tag__in'			=> [13]
    );
    
    $portfolios	= get_posts($args);

    Examining the resulting query with query monitor reveals that the post type ‘tribe_events’ is being added to the query.

    SELECT wp_posts.ID
    FROM wp_posts 
    INNER JOIN wp_term_relationships
    ON (wp_posts.ID = wp_term_relationships.object_id)
    WHERE 1=1 
    AND ( wp_term_relationships.term_taxonomy_id IN (13) )
    AND wp_posts.post_type IN ('portfolio', 'tribe_events')
    AND ((wp_posts.post_status = 'publish'))
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_date DESC

    If I remove the tag__in line the query works as expected, but with that line the ‘tribe_events’ post type shows up. If I disable The Events Calendar plugin the query works as expected with tag__in. I’ve also deactivated all plugins except the plugin running my query and the events calendar as well as switched to the default 2016 theme.

    #1122016
    Cliff
    Member

    Hi. Thanks for your detailed question.

    Are there any tribe_events posts (TEC Events) assigned to Tag ID 13?

    #1122141
    pleek91
    Participant

    Cliff,

    I have two events in my dev environment and neither of them have any tags.

    #1123080
    Cliff
    Member

    Here’s some information I got from one of our developers:

    Right now, things are constructed in such a way that queries referencing tags are expanded to include events in their scope, so what you’re experiencing is expected even if it isn’t ideal for your use case.

    This may be a workaround for you (or at least lead you toward a solution of your own):

    function remove_events_from_query( $query ) {
    $query->set( 'post_type', 'portfolio' );
    } );
    
    function disable_tribe_tag_query() { add_action( 'tribe_events_parse_query', 'remove_events_from_query' ); }
    
    function reenable_tribe_tag_query() { remove_action( 'tribe_events_parse_query', 'remove_events_from_query' ); }

    then…

    disable_tribe_tag_query();
    my_custom_query(); // (whatever you want to do here)
    reenable_tribe_tag_query();

    You may want to generalize it a little more to accommodate other cases

    FYI: you could inspect the existing value via $query->get( ‘post_type’ ) and only modify it if it includes tribe_events for instance, but the code above should be able to guide you in the right direction for you to customize it to your needs.

    Please let me know if this helped.

    #1129925
    Support Droid
    Keymaster

    This topic has not been active for quite some time and will now be closed.

    If you still need assistance please simply open a new topic (linking to this one if necessary)
    and one of the team will be only too happy to help.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘tribe_events post type being added to custom get_posts query’ is closed to new replies.