Home › Forums › General Issues › relationship filter – query by page-template?
hello,
i want to show only pages in my relationship field in backend that have a special template (“page-fach.php”). this is what i tried so far:
// relationship field "Fächer"
// the key of the field "Fächer"
add_filter('acf/fields/relationship/query/key=field_5593ec53f0a0c', 'my_relationship_query', 10, 3);
function my_relationship_query( $args, $field, $post )
{
// show only pages with this special template
$args['meta_query'] = array(
array(
'key' => '_wp_page_template',
'value' => 'page-fach.php',
'compare' => '!='
)
);
return $args;
}
but nothin happens, any idea what i am doing wrong?
maybe this helps to understand what i want to do – i want to show only pages here with the template “page-fach.php”.
If you want to match that template shouldn’t the compare value be =
compare' => '='
ah yes you’re right i was just trying everything like a monkey. when i write “=” i get “(no title())”
Are your templates in a sub folder of the theme? You’ll need to look at the actual value saved. For example on 2014 the value of the template setting is something like page-templates/full-width.php
hello john,
the template is in the theme folder like
wp-content/themes/mytheme/page-fach.php
not in a subfolder, it’s on the same level as the functions.php. i am making this theme from scratch.
maybe “_wp_page_template” doesn’t work with newer version sof wordpress? other arguments like e.g. “page_id” are working.
Have you tried something like
// relationship field "Fächer"
// the key of the field "Fächer"
add_filter('acf/fields/relationship/query/key=field_5593ec53f0a0c', 'my_relationship_query', 10, 3);
function my_relationship_query( $args, $field, $post )
{
// show only pages with this special template
$args['meta_key'] = '_wp_page_template';
$args['meta_value'] = 'page-fach.php';
return $args;
}
I don’t understand why, but it appears that you must supply a 'relation'
value. Try this
// relationship field "Fächer"
// the key of the field "Fächer"
add_filter('acf/fields/relationship/query/key=field_5593ec53f0a0c', 'my_relationship_query', 10, 3);
function my_relationship_query($args, $field, $post) {
// show only pages with this special template
$args['meta_query'] = array(
'relation' => 'AND',
array(
'key' => '_wp_page_template',
'value' => 'page-fach.php',
'compare' => '='
)
);
return $args;
}
After some more testing, it appears that I was wrong. The relation did not make any difference. The field worked perfectly when there was only one post returned by the query but it there are 2 or more matching posts then for some reason it fails. I am extremely confused now.
OK, I have figure out why this is failing.
If the post type is a hierarchical post type and your filter removes a parent post from the list of posts, for some reason this causes ACF have a problem. There is some type of error or something happening in the function acf_get_grouped_posts()
I would say that this is a bug, If you want I can give you a hack that will make it work but you’ll need to modify one of the ACF files. I will make the developer aware of this problem.
okay, awesome that you found this out! a hack would really help me! and thanks for informing the developer, i hope this problem can be fixed.
I can’t be sure this is the correct fix, the developer may do something different.
Change line 1463 of api/api-helpers.php to
if (is_post_type_hierarchical($post_type) && empty($args['s']) && empty($args['meta_query'])) {
in my case it is line 1518 and yes, this works – awesome. would be really nice if this could be fixed in a future release! thanks a lot john you are awesome!
Did the dev ever fix this issue, or is there an official workaround besides modifying the plugin core?
The topic ‘relationship filter – query by page-template?’ 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.