Support

Account

Home Forums Front-end Issues Include meta added via relational post object in an existing query function

Solving

Include meta added via relational post object in an existing query function

  • The following code had previously successfully included organizers added via my ACF Pro field to the The Events Calendar Pro’s tribe_organizer_upcoming_events loop.

    At some point—I’m not exactly sure when—the code ceased to do so. In other words, if an organizer was added to the event meta via the the_organizers ACF field, it is no longer being displayed on the individual’s bio page.

    Could anyone take a look at the once-working code, and let me know what may have changed and/or what now needs to be adjusted?

    The customized query function, which once worked successfully:

    <?php
    
    // Add all events to bio pages
    add_action( 'tribe_events_pre_get_posts', function( &$query ) {
        if ( $query->tribe_is_event_query && $query->get( 'organizer' ) != '' ) {
            // build our own version of the meta query for organizers
            $new_meta_query_term = array(
                array(
                    'key'   => '_EventOrganizerID',
                    'value' => $query->get( 'organizer' ),
                ),
                array(
                    // ACF field
                    'key'     => 'the_organizers',
                    'compare' => 'LIKE',
                    'value'   => '"' . $query->get( 'organizer' ) . '"'
                ),
                'relation' => 'OR'
            );
            $meta_query = $query->get( 'meta_query' );
            foreach( $meta_query as &$meta_query_term ) {
                // replace the meta query term that The Events Calendar made with our new one
                if( isset( $meta_query_term['key'] ) && $meta_query_term['key'] == '_EventOrganizerID' ) {
                    $meta_query_term = $new_meta_query_term;
                    break;
                }
            }
            $query->set( 'meta_query', $meta_query );
        }
    } );

    The exported PHP for the field group in question:

    <?php
    
    if( function_exists('acf_add_local_field_group') ):
    
    acf_add_local_field_group(array(
        'key' => 'group_5a26fec4071a0',
        'title' => 'The Organizers',
        'fields' => array(
            array(
                'key' => 'field_5a26ff1a9337b',
                'label' => 'Organizers',
                'name' => 'the_organizers',
                'type' => 'post_object',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'post_type' => array(
                    0 => 'tribe_organizer',
                ),
                'taxonomy' => array(
                ),
                'allow_null' => 1,
                'multiple' => 1,
                'return_format' => 'object',
                'ui' => 1,
            ),
            array(
                'key' => 'field_5b65c8357a7c1',
                'label' => 'Master of Ceremony',
                'name' => 'ceremony_master',
                'type' => 'post_object',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'post_type' => array(
                    0 => 'tribe_organizer',
                ),
                'taxonomy' => array(
                ),
                'allow_null' => 1,
                'multiple' => 1,
                'return_format' => 'object',
                'ui' => 1,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'tribe_events',
                ),
            ),
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'tribe_organizer',
                ),
            ),
        ),
        'menu_order' => 0,
        'position' => 'side',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => true,
        'description' => '',
    ));
    
    endif;

    The tribe_organizer_upcoming_events function:

    <?php
    
    /**
    	 * Output the upcoming events associated with a venue
    	 *
    	 * @return void
    	 */
    	function tribe_organizer_upcoming_events( $post_id = false ) {
    
    		$post_id = Tribe__Events__Main::postIdHelper( $post_id );
    
    		if ( $post_id ) {
    
    			$args = array(
    				'organizer'      => $post_id,
    				'eventDisplay'   => 'list',
    				'posts_per_page' => apply_filters( 'tribe_events_single_organizer_posts_per_page', 100 ),
    				'starts_after'   => 'now',
    			);
    
    			$html = tribe_include_view_list( $args );
    
    			return apply_filters( 'tribe_organizer_upcoming_events', $html );
    		}
    	}
  • Since you are interacting directly with the query class of the other plugin you best chance at a solution would be to ask support for that plugin. I can’t really tell by looking at your code because I don’t know how the other plugin works, but given what I know there isn’t any change is ACF that would have caused it to stop working.

  • Thanks John,

    I understand, and sincerely appreciate you taking the time to respond at all nonetheless.

    Unfortunately, the Tribe support staff has consistently proven quite eager to avoid providing any assistance said to fall beyond their “Scope of Support.” This was the gist of their response —

    I did look at your PHP file and it looks very similar (other than ACF logic) to what The Events Calendar is already doing: https://github.com/moderntribe/the-events-calendar/blob/4.9.1.1/src/Tribe/Query.php#L455-L461

    I’m not personally very familiar with ACF, but I know their codebase has received significant updates in recent history so maybe their logic isn’t as it used to be.

    If any easier, would it be possible to help me write a standalone query, for placement within the Tribe single-organizer.php template (both attached and pasted below), instead? Like the native tribe_organizer_upcoming_events function, the idea——in theory——would be for it to find any Tribe single event post where the organizer ID was added to the event meta, and return each given event ID.

    In other words, it would look for any Tribe event where the given organizer ID is selected via the ACF Pro field “the_organizers”.

    I’ve also attached a detailed screenshot for additional reference, as well as a few potentially relevant example links:

    Single Event Page

    Speaker | Organizer Page
    (set by native Tribe field–upcoming event display)

    Speaker | Organizer Page
    (set by ACF relational object field–upcoming event not display)

    <?php
    /**
     * Single Organizer Template
     * The template for an organizer. By default it displays organizer information and lists
     * events that occur with the specified organizer.
     *
     * This view contains the filters required to create an effective single organizer view.
     *
     * You can recreate an ENTIRELY new single organizer view by doing a template override, and placing
     * a Single_Organizer.php file in a tribe-events/pro/ directory within your theme directory, which
     * will override the /views/pro/single_organizer.php.
     *
     * You can use any or all filters included in this file or create your own filters in
     * your functions.php. In order to modify or extend a single filter, please see our
     * readme on templates hooks and filters (TO-DO)
     *
     * @package TribeEventsCalendarPro
     *
     * @version 4.4.28
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	die( '-1' );
    }
    
    $organizer_id = get_the_ID();
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    	<div class="tribe-events-organizer">
    			<p class="tribe-events-back">
    				<a href="<?php echo esc_url( tribe_get_events_link() ); ?>" rel="bookmark"><?php printf( __( '&larr; Back to %s', 'tribe-events-calendar-pro' ), tribe_get_event_label_plural() ); ?></a>
    			</p>
    
    		<?php do_action( 'tribe_events_single_organizer_before_organizer' ) ?>
    		<div class="tribe-events-organizer-meta tribe-clearfix">
    
    				<!-- Organizer Title -->
    				<?php do_action( 'tribe_events_single_organizer_before_title' ) ?>
    				<h1 class="tribe-organizer-name"><?php echo tribe_get_organizer( $organizer_id ); ?></h1>
    				<?php do_action( 'tribe_events_single_organizer_after_title' ) ?>
    
    				<!-- Organizer Meta -->
    				<?php do_action( 'tribe_events_single_organizer_before_the_meta' ); ?>
    				<?php echo tribe_get_organizer_details(); ?>
    				<?php do_action( 'tribe_events_single_organizer_after_the_meta' ) ?>
    
    				<!-- Organizer Featured Image -->
    				<?php echo tribe_event_featured_image( null, 'full' ) ?>
    
    				<!-- Organizer Content -->
    				<?php if ( get_the_content() ) { ?>
    				<div class="tribe-organizer-description tribe-events-content">
    					<?php the_content(); ?>
    				</div>
    				<?php } ?>
    
    			</div>
    			<!-- .tribe-events-organizer-meta -->
    		<?php do_action( 'tribe_events_single_organizer_after_organizer' ) ?>
    
    		<!-- Upcoming event list -->
    		<?php do_action( 'tribe_events_single_organizer_before_upcoming_events' ) ?>
    
    		<?php
    		// Use the tribe_events_single_organizer_posts_per_page to filter the number of events to get here.
    		echo tribe_organizer_upcoming_events( $organizer_id ); ?>
    
    		<?php do_action( 'tribe_events_single_organizer_after_upcoming_events' ) ?>
    
    	</div><!-- .tribe-events-organizer -->
    	<?php
    	do_action( 'tribe_events_single_organizer_after_template' );
    endwhile;
  • Thanks John,

    I understand, and sincerely appreciate you taking the time to respond at all nonetheless.

    Unfortunately, the Tribe support staff has consistently proven quite eager to avoid providing any assistance said to fall beyond their “Scope of Support.” This was the gist of their response —

    I did look at your PHP file and it looks very similar (other than ACF logic) to what The Events Calendar is already doing: https://github.com/moderntribe/the-events-calendar/blob/4.9.1.1/src/Tribe/Query.php#L455-L461

    I’m not personally very familiar with ACF, but I know their codebase has received significant updates in recent history so maybe their logic isn’t as it used to be.

    If any easier, would it be possible to help me write a standalone query, for placement within the Tribe single-organizer.php template (both attached and pasted below), instead? Like the native tribe_organizer_upcoming_events function, the idea——in theory——would be for it to find any Tribe single event post where the organizer ID was added to the event meta, and return each given event ID.

    In other words, it would look for any Tribe event where the given organizer ID is selected via the ACF Pro field “the_organizers”.

    I’ve also attached a detailed screenshot for additional reference, as well as a few potentially relevant example links:

    Single Event Page

    Speaker | Organizer Page
    (set by native Tribe field–upcoming event display)

    Speaker | Organizer Page
    (set by ACF relational object field–upcoming event not display)

    <?php
    /**
     * Single Organizer Template
     * The template for an organizer. By default it displays organizer information and lists
     * events that occur with the specified organizer.
     *
     * This view contains the filters required to create an effective single organizer view.
     *
     * You can recreate an ENTIRELY new single organizer view by doing a template override, and placing
     * a Single_Organizer.php file in a tribe-events/pro/ directory within your theme directory, which
     * will override the /views/pro/single_organizer.php.
     *
     * You can use any or all filters included in this file or create your own filters in
     * your functions.php. In order to modify or extend a single filter, please see our
     * readme on templates hooks and filters (TO-DO)
     *
     * @package TribeEventsCalendarPro
     *
     * @version 4.4.28
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	die( '-1' );
    }
    
    $organizer_id = get_the_ID();
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    	<div class="tribe-events-organizer">
    			<p class="tribe-events-back">
    				<a href="<?php echo esc_url( tribe_get_events_link() ); ?>" rel="bookmark"><?php printf( __( '&larr; Back to %s', 'tribe-events-calendar-pro' ), tribe_get_event_label_plural() ); ?></a>
    			</p>
    
    		<?php do_action( 'tribe_events_single_organizer_before_organizer' ) ?>
    		<div class="tribe-events-organizer-meta tribe-clearfix">
    
    				<!-- Organizer Title -->
    				<?php do_action( 'tribe_events_single_organizer_before_title' ) ?>
    				<h1 class="tribe-organizer-name"><?php echo tribe_get_organizer( $organizer_id ); ?></h1>
    				<?php do_action( 'tribe_events_single_organizer_after_title' ) ?>
    
    				<!-- Organizer Meta -->
    				<?php do_action( 'tribe_events_single_organizer_before_the_meta' ); ?>
    				<?php echo tribe_get_organizer_details(); ?>
    				<?php do_action( 'tribe_events_single_organizer_after_the_meta' ) ?>
    
    				<!-- Organizer Featured Image -->
    				<?php echo tribe_event_featured_image( null, 'full' ) ?>
    
    				<!-- Organizer Content -->
    				<?php if ( get_the_content() ) { ?>
    				<div class="tribe-organizer-description tribe-events-content">
    					<?php the_content(); ?>
    				</div>
    				<?php } ?>
    
    			</div>
    			<!-- .tribe-events-organizer-meta -->
    		<?php do_action( 'tribe_events_single_organizer_after_organizer' ) ?>
    
    		<!-- Upcoming event list -->
    		<?php do_action( 'tribe_events_single_organizer_before_upcoming_events' ) ?>
    
    		<?php
    		// Use the tribe_events_single_organizer_posts_per_page to filter the number of events to get here.
    		echo tribe_organizer_upcoming_events( $organizer_id ); ?>
    
    		<?php do_action( 'tribe_events_single_organizer_after_upcoming_events' ) ?>
    
    	</div><!-- .tribe-events-organizer -->
    	<?php
    	do_action( 'tribe_events_single_organizer_after_template' );
    endwhile;
  • Thanks John,

    I understand, and sincerely appreciate you taking the time to respond at all nonetheless.

    Unfortunately, the Tribe support staff has consistently proven quite eager to avoid providing any assistance said to fall beyond their “Scope of Support.” This was the gist of their response —

    I did look at your PHP file and it looks very similar (other than ACF logic) to what The Events Calendar is already doing: https://github.com/moderntribe/the-events-calendar/blob/4.9.1.1/src/Tribe/Query.php#L455-L461

    I’m not personally very familiar with ACF, but I know their codebase has received significant updates in recent history so maybe their logic isn’t as it used to be.

    If any easier, would it be possible to help me write a standalone query, for placement within the Tribe single-organizer.php template (pasted below), instead? Like the native tribe_organizer_upcoming_events function, the idea——in theory——would be for it to find any Tribe single event post where the organizer ID was added to the event meta, and return each given event ID.

    In other words, it would look for any Tribe event where the given organizer ID is selected via the ACF Pro field “the_organizers”.

    I’ve also included a link to a screenshot for additional reference, plus a few potentially relevant example links, below:

    Screenshot

    Single Event Page

    Speaker | Organizer Page
    (set by native Tribe field–upcoming event display)

    Speaker | Organizer Page
    (set by ACF relational object field–upcoming event not display)

    <?php
    /**
     * Single Organizer Template
     * The template for an organizer. By default it displays organizer information and lists
     * events that occur with the specified organizer.
     *
     * This view contains the filters required to create an effective single organizer view.
     *
     * You can recreate an ENTIRELY new single organizer view by doing a template override, and placing
     * a Single_Organizer.php file in a tribe-events/pro/ directory within your theme directory, which
     * will override the /views/pro/single_organizer.php.
     *
     * You can use any or all filters included in this file or create your own filters in
     * your functions.php. In order to modify or extend a single filter, please see our
     * readme on templates hooks and filters (TO-DO)
     *
     * @package TribeEventsCalendarPro
     *
     * @version 4.4.28
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	die( '-1' );
    }
    
    $organizer_id = get_the_ID();
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    	<div class="tribe-events-organizer">
    			<p class="tribe-events-back">
    				<a href="<?php echo esc_url( tribe_get_events_link() ); ?>" rel="bookmark"><?php printf( __( '&larr; Back to %s', 'tribe-events-calendar-pro' ), tribe_get_event_label_plural() ); ?></a>
    			</p>
    
    		<?php do_action( 'tribe_events_single_organizer_before_organizer' ) ?>
    		<div class="tribe-events-organizer-meta tribe-clearfix">
    
    				<!-- Organizer Title -->
    				<?php do_action( 'tribe_events_single_organizer_before_title' ) ?>
    				<h1 class="tribe-organizer-name"><?php echo tribe_get_organizer( $organizer_id ); ?></h1>
    				<?php do_action( 'tribe_events_single_organizer_after_title' ) ?>
    
    				<!-- Organizer Meta -->
    				<?php do_action( 'tribe_events_single_organizer_before_the_meta' ); ?>
    				<?php echo tribe_get_organizer_details(); ?>
    				<?php do_action( 'tribe_events_single_organizer_after_the_meta' ) ?>
    
    				<!-- Organizer Featured Image -->
    				<?php echo tribe_event_featured_image( null, 'full' ) ?>
    
    				<!-- Organizer Content -->
    				<?php if ( get_the_content() ) { ?>
    				<div class="tribe-organizer-description tribe-events-content">
    					<?php the_content(); ?>
    				</div>
    				<?php } ?>
    
    			</div>
    			<!-- .tribe-events-organizer-meta -->
    		<?php do_action( 'tribe_events_single_organizer_after_organizer' ) ?>
    
    		<!-- Upcoming event list -->
    		<?php do_action( 'tribe_events_single_organizer_before_upcoming_events' ) ?>
    
    		<?php
    		// Use the tribe_events_single_organizer_posts_per_page to filter the number of events to get here.
    		echo tribe_organizer_upcoming_events( $organizer_id ); ?>
    
    		<?php do_action( 'tribe_events_single_organizer_after_upcoming_events' ) ?>
    
    	</div><!-- .tribe-events-organizer -->
    	<?php
    	do_action( 'tribe_events_single_organizer_after_template' );
    endwhile;
  • Rather than do that let’s see if we can work out the issue.

    I am assuming that the line in their function

    
    return apply_filters( 'tribe_organizer_upcoming_events', $html );
    

    eventually causes your filter to be run.

    The first thing we need to find out is if your filter is running at all. Do you know that this is the case? If not then

    
    <?php
    
    // Add all events to bio pages
    add_action( 'tribe_events_pre_get_posts', function( &$query ) {
        
        // see if this filter is being called
        echo 'my filter was called'; die;
        
        if ( $query->tribe_is_event_query && $query->get( 'organizer' ) != '' ) {
            // build our own version of the meta query for organizers
            $new_meta_query_term = array(
                array(
                    'key'   => '_EventOrganizerID',
                    'value' => $query->get( 'organizer' ),
                ),
                array(
                    // ACF field
                    'key'     => 'the_organizers',
                    'compare' => 'LIKE',
                    'value'   => '"' . $query->get( 'organizer' ) . '"'
                ),
                'relation' => 'OR'
            );
            $meta_query = $query->get( 'meta_query' );
            foreach( $meta_query as &$meta_query_term ) {
                // replace the meta query term that The Events Calendar made with our new one
                if( isset( $meta_query_term['key'] ) && $meta_query_term['key'] == '_EventOrganizerID' ) {
                    $meta_query_term = $new_meta_query_term;
                    break;
                }
            }
            $query->set( 'meta_query', $meta_query );
        }
    } );
    
  • I can’t even begin to explain how much I appreciate your help, John. You, personally, are seen everywhere across this forum, going above and beyond to provide support to an extent that I don’t see from any support team at all (and doing so for arguably as valuable a plugin as exists). In all sincerity, thank you. It’s nothing short of amazing.

    Anyways, using the code you’ve provided above, I can confirm that, yes, the filter is in indeed being called.

  • Thank you, I do this for a couple of reasons, one of them being that I feel ACF is the most valuable WP plugin that exists and will become more so with the bad direction I feel that WP is going with their block editor.

    Anyway. Remove the line of code I added before and replace it with this

    
    if ($query->get('organizer')) {
      var_dump($query->get('organizer')); echo '<br />';
    }
    echo '<pre>'; print_r($query); die;
    

    and post whatever it is that outputs. There’s going to be a long nested query, but I need to know what’s in it.

  • This reply has been marked as private.
  • Did you include this part, I’d like to see what’s in $query->get('organizer')

    
    if ($query->get('organizer')) {
      var_dump($query->get('organizer')); echo '<br />';
    }
    
  • This reply has been marked as private.
  • This reply has been marked as private.
  • The value of the query variable is not being output, make sure that is before the die statement and not after…. unless I’m missing it. No need to post the query array again, just the value of that variable is all I need.

  • This reply has been marked as private.
  • You have a couple of problems here

    First is that $query->get( 'organizer' ) is returning nothing because it is not set. It appears that this has been changed to tribe_organizer. You’d thing they would tell you this after looking at your code. So, I don’t think that your condition is ever being met. Try this to find out

    
    if ( $query->tribe_is_event_query && $query->get( 'organizer' ) != '' ) {
      echo 'query var set'; die;
    

    I think that the second issue is that this value contains a holding a name and you are querying a post object field that contains a post ID.

    You need the post ID for this organizer and not the name.

    Not sure how to solve this.

    Tell me more about this query we’re trying to modify, it is a sub query? The information for the organizer has already been shown and now you are trying to show the events that they are organizing?

  • Hi John — I hope you had a nice, relaxing and well-deserved weekend.

    $query->get( ‘organizer’ ) is returning nothing because it is not set. It appears that this has been changed to tribe_organizer.

    You appear to be correct. With the code above in place, the die function is never returned. Once changed to get( 'tribe_organizer' ), “query var set” is returned.

    So I gave this a look:

    if ($query->get('tribe_organizer')) {
      var_dump($query->get('tribe_organizer')); echo '<br />';
    }

    Which returned:
    string(14) "alex-greenbaum"

    So —

    You need the post ID for this organizer and not the name.

    Given what you’ve explained to be what we’re looking for, would either of these solutions, if nothing else, be helpful starting points?

    Solution 1 (based on this native Tribe function tribe_get_organizer_ids)

    // search the ID of organizer
    $organizers = tribe_get_organizer_ids($id);
    
    // retrieve name of organizer
    // will create an events loop tribe_get_events
    $orgName = array();
    foreach($organizers as $organizerId) {
      $orgName[] = tribe_get_organizer($organizerId);
    }

    And, another here: Solution 2

    Additionally, all of the native Tribe organizer-related functions, straight from their documentation here.

    Tell me more about this query we’re trying to modify, it is a sub query? The information for the organizer has already been shown and now you are trying to show the events that they are organizing?

    That is correct. I am trying to display all of the events for which the organizer has been set as an organizer/speaker.

    The “Organizer” page, as it is natively called, is said to be a custom taxonomy.

    Bear with me here, because it may (almost certainly will) get a little confusing. I have changed the label of this post-type (or custom taxonomy?) to “Speakers | Organizers”. With some template customization, and the help of ACF Pro, from the single event edit screen, I use the native meta section to select what is being output under a “Speakers” header in the meta section on the front end, and a separate ACF Pro relational object field to select what is being output under an “Organizers” header in the meta section on the front end. Both the native meta object selection field and the ACF Pro selection field are pulling from the same pool of “Speakers | Organizers.”

    That’s all working as intended.

    By default, the individual Speaker | Organizers pages then display all upcoming events that the Speaker | Organizer is tied to. For a while, that solution I posted in the first post of this thread, ensured that the upcoming event was posted on the individual’s Speaker | Organizer, regardless of whether they were attached via the native tribe meta field or the ACF Pro relational field. Now, however, the upcoming events loop found on the individual Speaker | Organizer page is only pulling in that respective Speaker | Organizer’s upcoming event if it were set via the native meta field (while failing to recognize it as an upcoming event if set via the ACF Pro relational field).

  • So It really depends on if there is more that one organizer, this is really just a guess, but it adds all the organizer IDs to the query if there is more than one.

    
    
      
      $meta_query = array(
        'relation' => 'OR',
        array(
          // I don't know if this needs to be changed
          'key'   => '_EventOrganizerID',
          'value' => $query->get('tribe_organizer'),
        )
      );
      $organizers = tribe_get_organizer_ids($id);
      foreach ($organizers as $organizer) {
        // add each organizer
        $meta_query[] = array(
          'key' => 'the_organizers',
          'compare' => 'LIKE',
          'value' => '"'.$organizer.'"'
        );
      }
    
  • Thanks John.

    It’s entirely possible that I’m implementing your recently suggested code in a manner other than what you had in mind, but either way, still no luck. For what it’s worth, my attempted variations have included both 'value' => $query->get('tribe_organizer') and 'value' => $query->get('organizer').

    At this point, personal guilt with respect to your time is starting to creep in. If this has become too complicated, and/or if these efforts have come to require too much hand-holding, I completely understand. Just let me know.

    Regardless of the ultimate outcome, I can’t thank you enough for the time you’ve already dedicated to assisting me.

Viewing 18 posts - 1 through 18 (of 18 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.