• Resolved handlez36

    (@handlez36)


    Hi, I have seen an issue with querying WP via Rest API platform while including taxonomy parameters (categories, tags). These queries seem to bring back posts and tribe_events that share the tag. In my use case I only want posts for that taxonomy type. The query I see being run when I fire a request is as follows:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
    FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    WHERE 1=1 AND (
    wp_term_relationships.term_taxonomy_id IN (18)
    ) AND wp_posts.post_type IN (‘tribe_events’, ‘post’) AND ((wp_posts.post_status = ‘publish’))
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_date DESC
    LIMIT 0, 30

    I placed code to examine the SQL query in this file. …./wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php. Prior to the

    “$query_result = $posts_query->query( $args )”

    line running (and after the “apply_filters” call for “rest_{$this->post_type}_query”), I explicitly set $args[‘post_type’] = ‘post’; I would expect this to only pull posts, no tribe events.

    I’ve also validated that the “Include events in main blog loop” was disabled. It’s actually always been disabled, so I don’t think there could be any caching issues related to still seeing it.

    Any help around how to not have tribe_events post_type automatically inserted for Rest API queries would be appreciated!

    Thank you!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Darian

    (@d0153)

    Hi @handlez36

    Thanks for reaching out.

    Could you provide the version of the following? This will help me understand how your system is set up.

    • PHP
    • WordPress
    • The Events Calendar
    • Theme

    @d0153 , I’m having a similar issue. I have a custom plugin that contains several custom blocks for our site. On one of the blocks, I am using a WP query via the Rest API, and it also keeps pulling events into the query. Here is my set up:
    PHP 8.0
    WordPress 6.1.1
    The Events Calendar 6.0.8
    The Events Calendar Pro 6.0.7
    Theme – custom theme (although this seems to have no impact because I have been having issues on Twenty Twenty-Three and any other theme I have tried)

    Here is an example of the query:
    $posts = new WP_Query(array(
    “posts_per_page” => $count,
    “post_status” => “publish”,
    “post_type” => “faculty”,
    ‘tag’ => $tags,
    “orderby” => “rand”,
    ));

    I expect this query to grab our “faculty” custom posts that filters by supplied $tag and $count variables provided by the block. The query works when I remove the tag parameter. When the tag parameter is present, it pulls in all faculty and events with the tag.

    Thank you for any help you can provide.


    Thread Starter handlez36

    (@handlez36)

    Apologies for the late reply, but I found where the issue may be. Not sure if the plugin authors would define it as an issue, but there doesn’t seem to be a flag to indicate not to include tribe_events for post searches via API calls. There is a setting to not include them when you’re viewing the events outside of an Api context though (Admin_Helpers::instance()->is_post_type_screen( ‘post’ )), but not via an Api call.

    In the following file “wp-content/plugins/the-events-calendar/src/Tribe/Query.php”, there are the following lines…

    // Add Events to tag archives when not looking at the admin screen for posts.

    if (

    ! $any_post_type

    && $query->is_tag

    && ! $is_event_query

    && ! Admin_Helpers::instance()->is_post_type_screen( ‘post’ )

    ) {

    self::add_post_type_to_query( $query, TEC::POSTTYPE );

    }

    The API query I was using matched ALL of these conditions. I assume if this was to support hiding tribe_events specifically for Api access the Admin_Helpers instance should include a is_post_type_api_call() method or something, but it doesn’t look like one exists. So I just commented out the add_post_type_to_query call, and that seemed to work.

    I appreciate the thorough response. It seems we are having the same issue as it worked for me as well when I commented out that add_post_type_to_query call. Thank you for your help.

    I am curious what the plugin authors think about this problem. It would definitely be preferable to have a method to solve this without having to directly edit the Query.php file and managing how future updates might impact our edits.

    Plugin Support Darian

    (@d0153)

    @jchawkins725 @handlez36

    Thanks for your detailed response. Let me share this with the team and I’ll get back to you once I hear back from them.

    In the meantime, could you try adding this to your query tribe_suppress_query_filters => true, and check if that helps?

    Plugin Support Darian

    (@d0153)

    Hi @handlez36 @jchawkins725

    Thank you for patiently waiting.

    I wanted to give you an update on this issue. At this time, we have been able to reproduce the problem and have identified a potential fix. Our engineers are working on the solution, though there are some complexities, and it will take some time to craft and validate before release.

    I have added your case to our internal ticket. While we don’t have an ETA, you’ll get notified once there is an update and fix.

    Regards,
    Darian

    ———–
    Internal Bug Ticket Reference: TEC-4694

    @d0153

    Thank you for the follow up. I tried the following and it did work for my query.

    tribe_suppress_query_filters => true

    I hadn’t seen that option, so I appreciate you suggesting it. I’m glad to have a way to continue building without directly editing the plugin file. I’m looking forward to future updates.

    Thank you both for your help.

    Plugin Support Darian

    (@d0153)

    Hi @jchawkins725

    Thanks for confirming that the workaround works on your end.

    Please let me know if you have other questions or concerns.

    Gladys

    (@greventscalendar)

    Hello there,

    We’re back with some good news!

    We have fixed an issue using WP Query to get only normal WP posts that match a certain tag, events with that same tag are returned even though the query is set to just posts.

    Please update The Events Calendar to its latest version to apply these changes.

    As always, we recommend testing updates on a staging site first – https://theeventscalendar.com/knowledgebase/k/creating-and-using-a-wordpress-staging-site/ – and keeping a recent backup, but it should all be smooth sailing.

    Regards,
    Gladys

    Plugin Support Darian

    (@d0153)

    This thread has been inactive for a while, so we’ll go ahead and mark it Resolved. Please open a new thread if any other questions arise, and we’d be happy to help.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Rest post query with post_type = post automatically includes tribe_events type’ is closed to new replies.