DEV Community

riyaz7us
riyaz7us

Posted on

Cannot filter posts by ACF relationship values

The question belongs to Wordpress and Advanced Custom Fields.

I have created two post types i.e., 'courses' and 'universities'. In courses, I created a relationship 'related_university' that connects courses to universities. However, I could not filter my courses by relationship values (title or any other field).

the following method works for me when I print the title of the related university:

$result[] = array(
"id" => get_the_ID(),
"title" => get_the_title(),
"rel_university_name" => get_field("related_university")[0]->post_title,
);

However, I get no results when I use the same method to filter my posts by the title:

$args['meta_query'][] = array(
'key' => get_field('related_university')[0]->post_title,
'value' => $related_university_title,
'compare' => 'LIKE'
);

Top comments (2)

Collapse
 
vanaf1979 profile image
Stephan Nijman • Edited

First of you are passing a post_title for a meta field key, while the meta field in this case should just be 'related_university'.

Second the value for this meta field is probably a array of post id's or post objects, not a string like you are passing.

Acf has a nice post about this here: advancedcustomfields.com/resources...

And as a side note: While you can always ask your questions here, i think you have a better chance of a good answer if you post your questions on StackOverflow: stackoverflow.com/questions/tagged...

Collapse
 
riyaz7us profile image
riyaz7us

Thanks for your reply stephan, However, I was trying to filter my posts (courses in my case) by their related field's (university's) title or other field. I couldn't find a solution for relationship query except one for filtering by ID.