Hi, I’ve been working with the Querying Relationship Fields tutorial, and trying to perform a reverse query.
My setup is:
-A Relationship Field called “event_ticket_relationship” that ties a WooCommerce product (post type name is ‘product’) to an Events Calendar event (post type name is ‘event’).
-On the WooCommerce product, I’m trying to display some information from the event that the product has a relationship with.
So far, I’m coming up empty. Any suggestions?
<?php
// Add Course Info to WooCommerce Product
add_action( 'woocommerce_before_single_product_summary', 'kfi_course_info' );
function kfi_course_info() {
// Remove the Product Images on Single Product Page
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'training-events', $categories ) ) {
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
}
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
?>
<?php
$tickets = get_posts(array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'event_ticket_relationship', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
if( $tickets ): ?>
<?php echo '<div class="column one_half">'; ?>
<?php foreach( $tickets as $ticket ):
?>
<?php tribe_get_template_part( 'modules/meta/venue', $ticket->ID ); ?>
<?php
// Start Time
if( get_field('course_start_time', $ticket->ID) ): ?>
<i class="fa fa-fw fa-clock-o" aria-hidden="true"></i> Start Time: <?php the_field('course_start_time', $ticket->ID); ?><br>
<?php endif; ?>
<?php
// Instructor
if( get_field('course_instructor', $ticket->ID) ): ?>
<i class="fa fa-fw fa-user-circle" aria-hidden="true"></i> Instructor: <?php the_field('course_instructor', $ticket->ID); ?><br>
<?php endif; ?>
<?php
// Fees
if( get_field('course_fee', $ticket->ID) ): ?>
<?php
// vars
$coursefee = get_field('course_fee', $ticket->ID);
$testfee = get_field('course_test_fee', $ticket->ID);
$totalfee = ($coursefee + $testfee);
?>
<i class="fa fa-fw fa-usd" aria-hidden="true"></i> Course Fee: <?php the_field('course_fee', $ticket->ID); ?> <?php if( get_field('course_test_fee', $ticket->ID) ): ?> + Test Fee: <?php the_field('course_test_fee', $ticket->ID); ?><?php endif; ?> = $<?php echo($totalfee) ?><br />
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>
<?php endif; ?>
<?php
}
// End of Course Info for WooCommerce Product
?>
The topic ‘Getting Reverse Query Data from Relationship Field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.