Ok i’m going to see if i can explain this and the issue i’m having as clearly as possible: I’m using a relationship field to create packages each “package” contains a handful of “proposals” pulled from the proposals CPT. simple enough.
However while viewing an individual proposal, i’m attempting to reverse query the relationship field, to get the Package. This i’ve also been successful at.
The next part of the challenge is to then query that parent, for the contents of the relationship field, so i can create a sibling nav of the other proposals in the same package.
I also was able to accomplish this – and retrieve a list of the proposals in the same package. HOWEVER. This messed up the rest of the page, with the info being shown for proposal i’m viewing showing instead the info for the first sibling in the relationship. I have an idea that this is related somehow to $post and resetting post data – which I think i’m doing, but i’m just not sure why it isn’t working. If you could, have a look at my nav query below and let me know where i’m screwing up. Thanks ahead of time.
<ul class="package">
<?php
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => 'package-creator', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
);
// the query
$getmysiblings = new WP_Query( $args ) ?>
<?php if ( $getmysiblings->have_posts() ) : ?>
<?php while ( $getmysiblings->have_posts() ) : $getmysiblings->the_post(); ?>
<?php
$posts = get_field('package-creator');
if( $posts ): ?>
<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
<a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<?php endif; ?>
</ul>
Yeah.. My first thought was the $posts variable not resetting. Try sticking the <?php wp_reset_postdata(); ?> after the closing ul tag because it looks to me there may be some instances in that loop where it wouldn’t be reset or would be reset before you needed it to be.
Thanks for the input – I tried moving the reset to various points like recommended but it’s still causing the $post content to display instead of the proper page content. So odd.
What’s interesting is that it has something to do with the second query (the query for the contents of the relationship field. If i remove that query then i just get the page info for parent, and the rest of the page loads fine. It’s when i attempt a further query into that page that it messes with the $post data. I feel like the answer is right there… just not sure.
Mate! I’ve just realised you’ve missed something after your foreach begins you have to set up the post data. Surely that will fix the weird activity?
<?php foreach( $posts as $post ): ?>
<?php setup_postdata($post); ?>
Sorry. I’ve just looked at this further and it looks like you are messing up the two methods as mentioned in this page? http://www.advancedcustomfields.com/resources/relationship/
So the foreach you have used in your original code doesn’t need the <?php wp_reset_postdata(); ?> so you could remove that or you could use the first method in the linked example but you would have to restructure your $post and $p variable names.
Hope this helps!
Thanks magicstick – I’ve been playing with using both variations (get_post vs wp_query essentially) i’m going to try something else. I’ll report back with any progress.
It’ so interesting so – the code below is the reverse relationship query minus the (for lack of better terminology) internal relationship query. This works flawlessly, and finds the parent, without interrupting the current post’s data.
<ul class="package">
<?php
$args = array(
'post_type' => 'page',
'meta_query' => array(array(
'key' => 'package-creator',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
));
$getmysiblings = new WP_Query($args) ?>
<?php if ($getmysiblings->have_posts()): ?>
<?php while ($getmysiblings-> have_posts()): $getmysiblings->the_post(); ?>
<?php // get the relationship field ?>
THIS IS WHERE THE RELATIONSHIP QUERY SHOULD GO.
<?php // end get relationship field ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<?php endif; ?>
</ul>
I’ve tried using both variations of the relationship query inside of the above marked area – and both ways it works, but corrupts the content of the provided page. (which btw, is retrieving ACF fields not just post content) I’ll keep working on it.
It almost makes me think that I need to find an alternative for pulling the relationship field that doesn’t use another loop.
I rebuilt the query using get_posts entirely and still no love – again the query seems to be corrupting the main $posts. Might open up a ticket with Elliot.
I’ve got it!
Instead of running another full query – I just ran a local foreach.
<ul class="package">
<?php
$args = array(
'post_type' => 'page',
'meta_query' => array(array(
'key' => 'package-creator',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
));
$getmysiblings = new WP_Query($args) ?>
<?php if ($getmysiblings->have_posts()): ?>
<?php while ($getmysiblings-> have_posts()): $getmysiblings->the_post(); ?>
<?php // get the relationship field ?>
<?php
$siblings = get_field('package-creator');
foreach($siblings as $sibling) {
$sibling_name = $sibling->post_title; ?>
<li><a href="<?php echo post_permalink($sibling); ?>"><?php echo $sibling_name; ?></a></li>
<?php } ?>
<?php // end get relationship field ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<?php endif; ?>
</ul>
Hey @tanmccuin, I’m facing a very similar issue here, almost 4 years later π
I’m building a portfolio site for a video producer company. Directors and Videos are CPTs, and director’s page displays the videos linked to them via relationship field. I call these videos using the WP_Query method.
The video page might show the current video but also the previous and the next from that previous loop. So I reach your thread and things look basically the same.
Unfortunatelly, I’m not getting the same result as you π Could you give some help?
My single-video.php
file has this piece of code, same as yours:
$args = array(
'post_type' => 'director',
'meta_query' => array(array(
'key' => 'director-video',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
));
$getmysiblings = new WP_Query($args);
When I print_r
or vardump
the $getmysiblings
, the result is an error. What am I doing wrong?
My hunch is get_the_ID function isnβt returning anything. Var dump it out before query to check?
@magicstick thanks for the fast reply! I repasted code, adjusted to my current formatting and things suddenly worked. Confusing, pretty sure! π
One more thing, any ideas how to get just the previous and next from current? On the first and last, is it possible to get the first as next (when on last) and the last as previous (when on first).
Thanks!
next_posts_link( 'Older Entries', $getmysiblings->max_num_pages );
@magicstick sorry man, where should I put this? I placed it between endwhile
and wp_reset_postdata()
, but it’s not even showing on frontend.
The topic ‘Get siblings via parent relationship.’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.