Home › Forums › General Issues › Call posts based on relationship value
I am using a field type of “Relationship” (slug = department_news)
I have placed this custom field on all regular posts.
I am then able to add related “directory profiles” to my posts. “Directory Profiles” are custom post type I created for a directory of employees.
What I would like to do is call ALL POSTS that have a Relationship value of “David Smith” to them and display those posts on the Profile page.
Here’s the code I have so far:
$mine = array(
'meta_key' => 'directory_news',
);
// The Query
$the_query = new WP_Query( $mine );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts()) {
$the_query->the_post();
echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . get_the_title() . '</a></li> ';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
However, this just pulls all posts and not just the ones where “David Smith” has been chosen from the relationship field type.
How can I do that?
Is this even possible?
What I am trying to do is call ALL posts that have a relationship value that includes “David Smith”.
Where “David Smith” is a post inside a custom taxonomy called “directory”.
If i understand correctly you need to do a reverse query: http://www.advancedcustomfields.com/resources/querying-relationship-fields/
So what I have done is create a new custom post type (Directory)… and this allows me to then use ACF to add in all kinds of fields about our employees. I have already created a full directory, and generated individual profile pages.. That side of it is all good.
I added a ACF (relationship field) to my regular blog posts… this allows me to attach “profiles” that have been created inside my Directory post type. This lets me basically tag people from my directory inside my basic blog entries.
What I want to do, is display all posts where David Smith has been “tagged”… when I am viewing David Smiths profile page. (see photo for visual)
As mentioned above you’ll need to do a reverse query.
Understanding the query
The above “get_posts” query finds all posts that are of the type “doctor”. It then finds the “location” custom field for each doctor and runs a LIKE query on the value.
The relationship field saves it’s data as a serialized array. If you are not familiar with this format, please look up the stored value in your database. It will look something like this:
a:2:{i:0;s:2:”35″;i:1;s:2:”33″;}
This serialized array has 2 values, the first is “35”, the second is “33”. As this is the “location” field for a “doctor”, these 2 numbers must be the ID’s of location post types.In our query, the LIKE value was ‘”‘ . get_the_ID() . ‘”‘, if the location ID was 35 then the LIKE value would become ‘”35″‘. In this case, the query would match against the doctor’s location value (the serialized array shown above) and the doctor will be returned.
Ok… so I’ve tried it a few different way and am still missing it.
I checked my database and here is what my Hello World! post has in it for directory_news:
a:2:{i:0;s:2:"65";i:1;s:2:"23";}
So that’s showing me that profiles with the ID of 65 and 23 are linked to that post…
So then I thought I followed the example that you provided with this
$mine = get_posts(array(
'post_type' => 'post', // "post" because I'm calling regular blog posts?
'meta_query' => array(
array(
'key' => 'directory_news', // slug of custom field
'value' => '65', // hardcoded it just to test
'compare' => 'LIKE'
)
)
));
You’ll see that instead of using the ID I just hardcoded 65 in there just to see if I could only pull that one…
However this still spits out ALL posts.. and not the ones that have David in the relationship field.
The topic ‘Call posts based on relationship value’ 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.