简体   繁体   中英

Only show child pages of a specific page ID in an ACF Relationship Select Box

I'm building a music lesson company. I have a main 'Team' page with a bunch of child pages for each music tutor, this has some ACF stuff on it. On the site there's also a 'Lessons' page, which is a single page that features all the instruments that each open a modal box to show a bit of information when clicked on, part of that info includes these details:

  • Which tutors teach that specific instrument
  • The tutor/s name
  • The tutor/s photograph
  • A link to the tutor/s child page

Using ACF I've set up an initial relationship select box to get everything started, and the client can go to the lessons page and select which tutors teach which instrument, this is great but to reduce the chances of human error I'd only like my client to be able to select from child pages found under the 'Team' page.

The ACF documentation does offer some help, but this is to only select the child parents of the specific page you're currently editing - found here

I've tried editing the function to look like this so it only displays child pages from a specific page ID (in this case it's 11).

function my_relationship_query( $args, $field, $post_id ) {

// only show children of the current post being edited
$args = array(
'post_parent' => 11,
'post_type'   => 'any', 
'numberposts' => 10,
'post_status' => 'any' 
);
$children = get_children( $args );  

// return
return $args;

}

// filter for every field
add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);

But it isn't doing what I want it to, haha. I'm wracking my brains. My PHP experience is still in its adolescence, so any help or advice on where I'm going wrong would be fab. Thanks!

It looks like you are returning the $args rather than the $children . Try something like this:

function my_relationship_query( $args, $field, $post_id ) {

    $args = array(
        'post_parent' => $post_id, // updated to use the current $post_id
        'post_type'   => 'any', 
        'numberposts' => 10,
        'post_status' => 'any' 
    );

    $children = get_children( $args );  

    // return
    return $children; // updated to return $children

}

// filter for every field
add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM