Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global $post;
- $parentID = $post->post_parent; //Shows 2068
- $currID = $post->ID; //Shows 2069
- $args = array('post_parent' => $post->ID, 'post_type' => 'projects', 'numberposts' => -1 );
- $children = get_children($args);
- foreach ($children as $child):
- $childIDs .= " ".$child->ID; //Get Children IDs
- endforeach;
- $allIDs = $parentID." ".$currID.$childIDs; //shows 2068 2069 2070 2071
- add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments');
- function wpb_show_current_user_attachments( $query, $allIDs ) {
- $allIDarray = explode (" ", $allIDs);
- //$user_id = get_current_user_id();
- if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts') ) {
- // $query['author'] = $user_id;
- $query['post_parent'] = $allIDarray;
- }
- return $query;
- }
- function restrict_media_images_per_post_type($query) {
- add_filter('posts_where', 'media_posts_where');
- add_filter('posts_join', 'media_posts_join');
- return $query;
- }
- add_filter('ajax_query_attachments_args', 'restrict_media_images_per_post_type');
- function media_posts_where($where) {
- global $wpdb;
- $post_id = false;
- $whitelist_post_type = array(
- 'post',
- '{custom post type}' //change this according to your need e.g. projects
- );
- if ( isset($_POST['post_id']) ) {
- $post_id = $_POST['post_id'];
- $post = get_post($post_id);
- if ( $post && in_array($post->post_type, $whitelist_post_type)) {
- $where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
- //$where .= $wpdb->prepare(" AND my_post_parent.post_type = %s AND {$wpdb->posts}.post_parent = %d", $post->post_type, $_POST['post_id']); //Use this if you want to restrict to selected post only
- }
- }
- return $where;
- }
- function media_posts_join($join) {
- global $wpdb;
- if ( isset($_POST['post_id']) ) {
- $join .= " LEFT JOIN {$wpdb->posts} as my_post_parent ON ({$wpdb->posts}.post_parent = my_post_parent.ID) ";
- }
- return $join;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement