0

In Advanced Custom Fields, I would like limit the Relationship field (http://www.advancedcustomfields.com/resources/relationship/) to specific pages.

For various resons, involing Custom Post Types is not a solution for me right now.

This is my sitemap:

website.com
website.com/contact/
website.com/help/
website.com/map/
website.com/hunting-products/
website.com/hunting-products/scopes/
website.com/hunting-products/rifles/
website.com/fishing-products/
website.com/fishing-products/fishing-rods/
website.com/fishing-products/bait/

In the Relationship field, and I would like to filter this so you only can select these products.

website.com/hunting-products/scopes/
website.com/hunting-products/rifles/
website.com/fishing-products/fishing-rods/
website.com/fishing-products/bait/
1
2

Found a solution that works:

add_filter('acf/fields/relationship/query/name=products', 'exclude_id', 10, 3);

function exclude_id ( $args, $field, $post ) {

    $args['post__not_in'] = array( $post, 9, 10, 11 );

    return $args;
}

Added in functions. This will exclude the pages with id 9, 10 and 11.

0

In register_taxonomy you can pass page as the associated post type and it will become an option on the edit screen

register_taxonomy( 'custom_tax', 'page', $args );

http://codex.wordpress.org/Function_Reference/register_taxonomy

1
  • I can already choose pages, so that is not them problem. I would rather want to block out pages that is not using the template I want.
    – Liu Kang
    May 7 '15 at 23:09
0

I had the quite similar issue. I needed to restrict Relationship Fields Choices to posts owned by Current User but grant a full access to “editor” & “administrator”.

Here is the code inserted in the functions.php file

<?php
function modify_field( $args, $field,$post) {
    if( !check_user_role(array('editor','administrator')) ){
        $args['author'] = get_current_user_id();
    }
    return $args;
}

    add_filter( 'acf/fields/post_object/query/key=field_XXX..XXX', 'modify_field', 10, 3 );

/* @src & @credits https://wp-mix.com/wordpress-check-user-roles/ */
function check_user_role($roles, $user_id = null) {
    if ($user_id) $user = get_userdata($user_id);
    else $user = wp_get_current_user();
    if (empty($user)) return false;

    foreach ($user->roles as $role) {
        if (in_array($role, $roles)) {
            return true;
        }
    }
    return false;
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.