Skip to content

Instantly share code, notes, and snippets.

@deguchi
Created July 19, 2017 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deguchi/f742dbdd44285ad8a2c2b8ff8ba26b74 to your computer and use it in GitHub Desktop.
Save deguchi/f742dbdd44285ad8a2c2b8ff8ba26b74 to your computer and use it in GitHub Desktop.
// this will effect all relationship fields
// if you only want to effect specific fields
// see https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
add_filter('acf/fields/relationship/query', 'relationship_only_own_posts', 10, 3);
function relationship_only_own_posts($args, $field, $post_id) {
// ACFのcourseの値で、表示する前提条件を絞る
$args['meta_key'] = 'course';
$post = get_fields($post_id);
$post_course = $post["course"];
//error_log(print_r($post_course->ID, true));
// error_log($post_id);
$args['meta_value'] = $post_course->ID;
// このエントリーを候補に出さない
$args['post__not_in'] = array($post_id);
// update core is something only available to admin
// or super admin on mutlisite
// a lot less code than looking through roles
if (current_user_can('update_core')) {
// not on admin or editor
return $args;
}
$author = get_current_user_id();
$args['author'] = $author;
return $args;
}
function sb_add_cpts_to_api() {
global $wp_post_types;
// Add CPT slugs here
$arr = ['course','mission'];
foreach( $arr as $key ) {
// If the post type doesn't exist, skip it
if (!$wp_post_types[$key]) {
continue;
} else {
$wp_post_types[$key]->show_in_rest = true;
$wp_post_types[$key]->rest_base = $key;
}
}
}
add_action( 'init', 'sb_add_cpts_to_api', 30 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment