Hi,
I am working on integrating a reverse relationship query into a custom query loop blocked by Generatepress.
I have the code that should work (support from GP confirmed it) but in reality, it doesn’t execute.
Here is my setup:
On my website, I have custom post type Trips and custom post-type Instructors. Each trip has a relationship ACF field that lets the admin select the Instructor for the trip (the relationship field is set to Post ID.)
Any thoughts on why it is not working?
On the instructor page, I need to use the Query Loop block to show all of the trips that this specific instructor is leading.
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
$block_class = 'trip-rel';
$related_field = 'select_the_pros';
if (
// if the quuery loop grid has $block_class
! is_admin() &&
! empty( $attributes['className'] ) &&
strpos( $attributes['className'], $block_class ) !== false
) {
global $post;
// get the $related_field
$related_trips = get_posts(array(
'post_type' => 'trip', //custom post type that has the relationship field configured for
'meta_query' => array(
array(
'key' => $related_field, // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
),
'fields' => 'ids'
));
var_dump($related_trips); //returns ids of the correct posts
return array_merge( $query_args, array(
$query_args['post_type'] = 'post',
$query_args['post__in'] = $related_trips,
$query_args['orderby'] = 'post__in'
));
}
return $query_args;
}, 10, 2 );
I’m guessing here because I cannot see how the loops over the posts are done. You are probably having an issue do to nested loops. There is the first loop “The Loop” which is the main loop done by WP, then you have a second loop over the “instructors” nested in this loop and finally you have a “trip” loop nested in that loop. Generally at the end of a nested query loop a the function wp_reset_postdata() is called to reset the post to the previous loop, but when working with nested loops this breaks the previous loop. The reason for this is the wp_reset_postdata() always resets the post to the primary WP loop and not any intermediate loops.
// primary WP loop
while (have_posts) {
// secondary loop
while ($secondary_query->have_posts()) {
// tertiary loop
while ($tertiary_query->have_posts()) {
}
// this call will not reset post data to post of $secondary query
// this function always resets post data to primary loop post
wp_reset_postdata();
// in this case instead of this function you must call
$secondary_query->wp_reset_postdata();
}
wp_reset_postdata(); // resets to primary loop post
}
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.