Home › Forums › General Issues › Relationship Field Entries Not Showing
Using current ACF.
CPT 1: Reviews
CPT 2: People
In Reviews, created three Relationship fields:
Cast
Writers
Directors
Added those to a Field Group for Reviews.
Entered all crew and cast entries in People CPT.
Went to Reviews. Selected and entered the correct People posts into the three Reviews Relationship fields. Saved.
Went back later. No visible entries in any of the three fields. Made entries again. Came back. They’re gone again.
One symptom. Using the GeneratePress Query Loop and their specialized filter code, I am able to get they query in a Reviews page to reference the People CPT and the Cast CPT. It populates with all of the entries I entered the first time. Entries from all three relationship fields, not just the Cast CPT, populates from the query.
Since the Relationship field entries disappearing from the Field post is all ACF, starting with you guys. There appears to be something in the database, but it’s not visible or editable.
Hopefully it’s a basic configuration error on my part, but I’ve not strayed off the path from the UI.
You have something interfering with ACF and this is difficult to debug in a forum.
If you remove the “GeneratePress Query Loop and their specialized filter code” does the issue resolve?
If the answer is yes then that’s where you need to look.
If not then you need to deactivate other plugins and maybe try a different theme until you figure out where the conflict is.
Hi John,
Thanks for the reply. I was just about to come back here and note that, indeed, the issue was a collision in similarly named taxonomies (early try, but inappropriate) and the relationship fields. Once I cleared that up, it’s working as expected.
If I may, one followup question?
Use Case 1: A query loop for a CPT1 Reviews content layout is pointing to CPT2 People. And for each of the three relationship fields, there’s a PHP function to query the relationship field and return data (a list of People post titles). Now working.
Use Case 2: I should be able to leverage the same technique? That is, a content layout for CPT2 People a query loop that points to CPT2 Reviews. And for each of the three relationship fields, a PHP function can query the same relationship fields and return data (a list of Review post titles). The same PHP functions in Use Case 1 don’t appear to work. A specific PHP script also didn’t work.
In the ACF relationship field I’ve made sure “Filter by Post Type” is set to both CPT2.
Anything else that might affect this? (I’m assuming that this is not a use case for bi-directional relationships, which appears to be more about enabling data changes to flow back and forth.)
I’m really just asking if the theory of leverage the relationship fields in CPT1 Reviews is correct here and it’s just a matter of tweaking the PHP until the return in a CPT2 People is accomplished.
Thanks again!
If it helps, here’s the filter that is working for Use Case 1. The Query Loop itself is configured to point to the Review CPT. Then a custom class (in this case, btc-writers-roles-relationship) is added to the Grid element under that. The PHP filter script is called when it sees the custom class name, plus the relationship field in the current Reviews CPT post (in this case, writers), and enables the block to reference fields to return and format in the block UI. Can you suggest changes that would make this work in “reverse” … the Use Case 2 above?
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
// find the block with <code>btc-writers-relationship</code>
if (
! empty( $attributes['className'] ) &&
strpos( $attributes['className'], 'btc-writers-relationship' ) !== false &&
! is_admin()
) {
// get the relate post ID
$queried_object = get_queried_object () ;
$relationship = get_field( "writers", $queried_object );
// Merge the current $query_args with the new args
return array_merge( $query_args, array(
'post__in' => $relationship,
) );
}
return $query_args;
}, 10, 2 );
I would suggest using bidirectional relationship fields. There is code available on this site as well as several plugins that offer this. The latest version of ACF also provides bidirectional support for relationship fields.
Queries for relationship fields without using bidirectional relationships are complex and I am unsure if you can to them using that filter. https://www.advancedcustomfields.com/resources/querying-relationship-fields/
Hi John,
Thanks for the guidance. Based on that, I went back to the People CPT, added Relationship fields, and linked them to the appropriate Review CPT fields. And with the customized GB filter for each instance, we’re seeing lists of Review titles on the People pages generated by the GB query loop. There are still some refinements to make when the Relationship field is blank. Fingers crossed that it will be something easily fixable. But in case, can you point me to any block-based plugins that support both use cases “out of the box” so to speak?
I’ll make the case resolved though, with thanks for the clarifications that put me on the right course.
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.