Home › Forums › General Issues › Only list pages with specific value in property in relationship field
I have pages with a dropdown list containing 3 choices.
These 3 choices defines the type of the page, and I’m listing out the different types each in their own relationship field.
The pages have a parent – child relationship in the page structure, like this:
type1
|--type2
| |--type3
|--type2
|--type2
|--type2
| |--type3
type1
|--type2
...
Basically, in the page structure, type 1 is the grandparent, type 2 the parent, and type 3 is the child.
This is what I am doing now on the acf/fields/relationship/query/name=
event:
function filter( $args, $field, $post_id ) {
$args['post_type'] = 'myposttype';
$args['meta_query'] = array(
array(
'key' => '_type', // The name of the dropdown with the types
'value' => 'type2', // The value selected in the dropdown
'compare' => '='
)
);
return $args;
}
// filter for every field
add_filter('acf/fields/relationship/query/name=relationship_field', 'filter', 10, 3);
Here I want to get all pages with the _type
dropdown set to type2
.
And it works, sort of.
The problem is when a page is in the grandparent place in the hierarchy, but its type is set to type2, in stead of type1.
Like this:
|-type1
| |--type2
| | |--type3
| |--type2
| |--type2
| |--type2
| | |--type3
|-type2
|-type1
|--type2
...
Here the type2
towards the bottom is in the grandparent place.
What happens then is that the relationship fields which gets all pages with the _type
dropdown set to type2
only returns the grandparent type2
, and leaves out all the rest.
It is like the acf/fields/relationship/query/name
filter only returns the hits on the outmost level in the page hierarchy.
If I changed the grandparent type2 to type1, it would correctly return all the type2 pages from the parent position in the structure.
Can anyone help?
I just want all the pages where _type
is set to type2
to be listed out in a relationship field. I’ve tried to create my own WP_Query
like this:
$a = new \WP_Query([
'post_type' => 'myposttype',
'meta_query' => array(
array(
'key' => '_type',
'value' => 'type2',
'compare' => '='
)
)
]);
var_dump($a->posts);
This returns correctly, so it seems like ACF is doing something to alter the results.
There is a known problem with the relationship field and hierarchical post types. It’s not really considered a bug. When ACF gets the posts in a hierarchical post type it attempts to short them hierarchically.
so in your condition
|-type1
| |--type2
| | |--type3
| |--type2
| |--type2
| |--type2
| | |--type3
|-type2
|-type1
|--type2
the grandparent pages that are “type1” are not returned and when ACF tries to sort them since the grandparents are missing at the top level the sub pages disappear. This is not the case when all of the grandparents are all of the “type1” type because all of the “parent type2” pages are returned.
There isn’t any way that I know of to fix this except to change a core ACF file. I put a fix for another topic here https://support.advancedcustomfields.com/forums/topic/relationship-filter-query-by-page-template/.
The topic ‘Only list pages with specific value in property in relationship field’ 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.