Code Element Tf

ACF Relationship Field: Query Pages by Page Template

Use this code snippet to query pages by page template in an Advanced Custom Field Relationship Field.


field_name: with the name of your ACF Relationship field

template-custom.php: with the path to your target page template

add_filter('acf/fields/relationship/query/name=field_name', function ($args, $field, $post_id) {
    // show only pages with this custom template
    $args['meta_key'] = '_wp_page_template';
    $args['meta_value'] = 'template-custom.php';
    return $args;
}, 10, 3);