Home › Forums › General Issues › Reverse Relationship Meta Query
I know, I know. This question has been asked 1,000 times. I’ve read every answer. Still can’t figure it out.
I have two CPTs, one for banks and one for bank accounts. One bank has many bank accounts.
On the bank account CPT, I have a relationship field that ties bank to banks.
On the bank single page, I need to list out all bank accounts associated with that bank.
Can anybody point me to or offer a clear explanation of how to do this? Thanks.
You must use WP_Query to query the “accounts” post type using a meta query to query by the “bank” relationship field.
// simplest form
$args = array(
'post_type' => 'accounts', // replace w/your post type
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'bank' // replace with your relationship field name
'value' => '"'.$post=ID.'"', // global post ID of current post
'compare' => 'LIKE'
)
)
);
Thank you for the reply. This is exactly the code I’ve been using with no luck. Curious if it matters what return format I use for the relationship field.
I should add that the code doesn’t work even if I hard-code the post id.
Are you using a relationship field or a post object field?
Does the field allow multiple selections or a single selection?
A relationship field, and it doesn’t allow for multiple selections.
So, that shouldn’t matter.
Just to double check…
The relationship field is located on the account PT editor page?
This relationship page is not a sub field of a group, repeater, or flex field?
Correct on both accounts. The relationship field is on the account CPT and points to the Bank CPT. It’s not a sub field, group, repeater or flex field.
Please provide your code including the context in which it is used, in what file the code is added.
The code is part of the bank CPT single-post template. I’ve tried many iterations, but here’s where it currently stands:
<?php
/*
* Template Name: Bank Review Test
* Template Post Type: banking
*/
add_action ( 'genesis_before_entry_content', 'banking_data');
function banking_data(){
$post_id = get_the_ID();
$accounts = get_posts(array(
'post_type' => array('bank_accounts','money_market_account','checking_accounts'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'bank_relationship', // name of custom field
'value' => $post_id, // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE',
)
)
));
if( $accounts ):
?>
<ul>
<?php foreach( $accounts as $account ): ?>
<li>
<?php echo get_the_title( $account->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
}
genesis();
?>
Note that the 3 post types all use the same ACF relationship field that points to the bank CPT. I’ve also tried hard-coding the post ID with no success.
okay, how is it not working, is it returning unrelated posts or is it not returning any posts?
This line in your code
'value' => $post_id,
should be
'value' => '"'.$post_id.'"',
the double quotes are important, see the comment in your code. ACF stores a relationship field as a serialized array of post IDs as string values. Searching for just a 1 without quotes would return 1, 12, 21, 123, 312, 321, 67549145, anything with a 1 in the post ID. Also, a serialize array stores the length of the string, so it would also return all posts with a post ID whose string length contains the post ID that you are searching for.
It’s not returning anything, and a var_dump of $accounts returns an empty array.
Also, I had been using the double quotes in past iterations and it doesn’t solve the problem. I’ve added them back into the code per your comment above.
I’ve looked into this and I can’t see any reason that it should not be working, unless it has something to do with using get_posts() instead of WP_Query(). My next step would be to try switching to WP_Query().
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.