Hi, Did any one made this exemple code working ?
This is not working on my side, and kind of driving me crasy.
I have a custom post called expertises.
In post, I have a relation field called “related_expertises”, where you can add different custom post expertises.
When I’m in single-expertises, I want to show all the post that have added this particular expertise to the relation filed.
$related = get_posts(array( ‘post_type’ => ‘post’, ‘meta_query’ => array( array( ‘key’ => ‘related_expertises’, ‘value’ => get_the_ID() , ‘compare’ => ‘LIKE’ )) ));
Based on the exemple this should work but return nothing. Did I get something wrong ?
Thanks for the help !
Thanks for taking the time.
I removed it because of this (last comment)
It’s still not working. But it should right ?
I’m wondering if I did something backward or totally miss something here.
That depends on the type of relationship field. A relationship field that only allows 1 selections is stored as the post ID of that selection and then you would query for posts there the value “=” that post ID. A relationship field that allows multiple values is stored as a serialized array and requires the “” around the post ID.
Are you using guberbug blocks? Is the relationship field a block?
If not the above, is the relationship field a sub field of another field; repeater, group, flexible content?
IN does not work, it is stored as a serialized array. IN only works when a meta field is stored in multiple DB entries. LIKE must be used for serialized arrays.
Are all the selected posts in the relationship published?
If that’s not it then the only thing I can think of is that the value of
get_the_ID() where it is called is not the post ID you think it is. Is this in any type of a nested post loop?
No, It’s not a weird loop, it’s just after the regular loop.
I’m thinking of an other way to solve my problem.
I can revert the system, and have the post in a relation ship field in the expertises custom post instead. Then I just need to call the get_field(‘related_post’) and i will have my array of id’s.
It will be simpler, and maybe even more logic for the redactors.
I’m going to grab some food then get back to it.
I will tell you how it went. Thanks for the help, I really appreciate it.
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!
The ACF site is currently experiencing some issues.— Advanced Custom Fields (@wp_acf) September 21, 2022
We apologize for the inconvenience and we are working hard to get it back up and running as soon as possible.
© 2022 Advanced Custom Fields.