Related events not showing their custom fields and excerpts.

Home Forums Calendar Products Events Calendar PRO Related events not showing their custom fields and excerpts.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #999577
    Lee
    Participant

    Hello, I’m trying to pull in extra information for the related events feed at the bottom of the individual event page. However, they just display the information relating to the event page that they are being used on. For example, I want to be able to get the event taxonomy, the event custom fields, and the cost of the event. Below is the code that I have modified in the related-events.php file in the pro plugin:

    <?php foreach ( $posts as $post ) : ?>
    			<?php setup_postdata($post); ?>
    			<li>
    				<div class="tribe-related-event-info list-event-inner">
    					<div class="top-info-wrap" data-equalizer-watch>
    						<?php
    							$reltags = get_the_tags($post->ID);
    							if($reltags){
    								$rtcount = count($reltags);
    								$rtccount = 1;
    								echo '<p class="course-tags">';
    								foreach ($reltags as $reltag) {
    									echo $reltag->name;
    									$rtccount++;
    									if($rtccount < $rtcount) {
    										echo ', ';
    									}
    								}
    								echo '</p>';
    							}
    						?>
    						<?php $thisun = $post->ID; ?>
    						<h3 class="tribe-related-events-title summary"><a href="<?php echo tribe_get_event_link( $post ); ?>" class="url" rel="bookmark"><?php echo get_the_title( $post->ID ); ?></a></h3>
    						<p class="entry-summary"><?php echo get_post($thisun)->post_excerpt; ?></p>
    					</div>
    					<ul class="detail-list">
    						<li>
    							<div class="icon-holder">
    								<i class="fa fa-briefcase"></i>
    							</div>
    							<?php
    							$professions = tribe_get_event_taxonomy($thisun);
    							$professions = str_replace('</li><li>',', ',$professions);
    							$professions = strip_tags($professions);
    							?>
    							<p><?php if($professions){ echo $professions; } else { echo 'TBC'; }?></p>
    						</li>
    						<li>
    							<?php $fields = tribe_get_custom_fields($thisun); ?>
    							<div class="icon-holder">
    								<i class="fa fa-hourglass-o"></i>
    							</div>
    							<?php $duration = $fields['Duration'] ?>
    							<p><?php if($duration) { echo $duration; } else { echo 'TBC'; } ?></p>
    						</li>
    						<li>
    							<div class="icon-holder">
    								<i class="fa fa-money"></i>
    							</div>
    							<?php $thecost = tribe_get_cost( null, true ); ?>
    							<?php if ($thecost) { echo '<p>' . $thecost . '</p>'; } else { echo '<p>TBC</p>'; } ?>
    						</li>
    					</ul>
    				</div>
    			</li>
    			<?php endforeach; ?>

    The event posts are custom post types aren’t they? Would it be better to use wp_query and specify the custom post type? I managed to get the excerpt by getting the whole post object but it seems a bit heavy handed.

    Many thanks for any information that points me in the right direction. I’m essentially wanting to access all the post information I can access when in the list view.

    #999955
    George
    Participant

    Hey @Lee,

    Thanks for posting all this information here and the relevant code itself! I’d like to first put out the disclaimer that we don’t officially support custom code and all that, just to set expectations early…

    However, regardless of that I think the issue here is actually a rather simple one, and am glad to help fix it!


    The Problem

    I believe that the extent of the problem here is just the use of setup_postdata() – now, this is not a mistake on your end, you’re not doing anything wrong. I’m sure you’ve found online and in other pieces of code that this the “normal” thing to do, and it is when looping through posts.

    However, I think in the context of the related-events.php file specifically, the $post variable just ends up referring to, as you noted, the parent event post itself, and not the three “related events post”.

    Why is this happening? I have some ideas but am not 100% certain. Regardless of that, however, you should be able to fix this by simply referring to the $post directly within that related events loop, while omitting that setup_postdata() call altogether.


    The Solution

    As noted, basically:
    • omit setup_postdata()
    • replace references to the “setup” $post with direct references to the current, “un-setup” post object in the loop.

    If you’re not familiar with the WP_Post object, which stores all the info like title, content, etc., and is the object we’ll be referencing, please read a bit about it here if you’re curious → https://codex.wordpress.org/Class_Reference/WP_Post

    That lists all the variables you can call from it.

    As an example of how to rewrite your code according to what I suggested above, here’s actually just all of your code, rewritten 🙂 → https://gist.github.com/ggwicz/f6ca93112dd2d1d394b0

    Try replacing your existing code with that and see if it helps!

    Thanks,
    George

    #1000173
    Lee
    Participant

    Hi George, thank you very much for the reply and your solution. I managed to fix this late on yesterday by adding the following snippet just before the $posts variable is defined:

    global $post, $wp_query;

    Thanks again though!

    #1000722
    George
    Participant

    Woah, much simpler than my proposed solution! 😀

    Apologies for how far off I was on things, but glad you got this sorted out! Best of luck with your site.

    George

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Related events not showing their custom fields and excerpts.’ is closed to new replies.