SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global $post;
  2.  
  3. $parentID = $post->post_parent; //Shows 2068
  4. $currID = $post->ID; //Shows 2069
  5.  
  6. $args = array('post_parent' => $post->ID, 'post_type' => 'projects', 'numberposts' => -1  );
  7.         $children = get_children($args);
  8.  
  9.         foreach ($children as $child):
  10.             $childIDs .= " ".$child->ID; //Get Children IDs
  11.         endforeach;
  12.  
  13. $allIDs = $parentID." ".$currID.$childIDs; //shows 2068 2069 2070 2071
  14.  
  15. add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments');
  16.  
  17. function wpb_show_current_user_attachments( $query, $allIDs ) {
  18.  
  19.     $allIDarray = explode (" ", $allIDs);
  20.     //$user_id = get_current_user_id();
  21.  
  22.     if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts') ) {
  23.  
  24.         // $query['author'] = $user_id;
  25.         $query['post_parent'] = $allIDarray;
  26.  
  27.     }
  28.     return $query;
  29. }
  30.      
  31. function restrict_media_images_per_post_type($query) {
  32.     add_filter('posts_where', 'media_posts_where');
  33.     add_filter('posts_join', 'media_posts_join');
  34.     return $query;
  35. }
  36. add_filter('ajax_query_attachments_args', 'restrict_media_images_per_post_type');
  37.      
  38. function media_posts_where($where) {
  39.     global $wpdb;
  40.     $post_id = false;
  41.     $whitelist_post_type = array(
  42.         'post',
  43.         '{custom post type}' //change this according to your need e.g. projects
  44.     );
  45.     if ( isset($_POST['post_id']) ) {
  46.         $post_id = $_POST['post_id'];
  47.         $post = get_post($post_id);
  48.         if ( $post && in_array($post->post_type, $whitelist_post_type)) {
  49.             $where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
  50.             //$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
  51.         }
  52.     }
  53.     return $where;
  54. }
  55.      
  56. function media_posts_join($join) {
  57.     global $wpdb;
  58.     if ( isset($_POST['post_id']) ) {
  59.         $join .= " LEFT JOIN {$wpdb->posts} as my_post_parent ON ({$wpdb->posts}.post_parent = my_post_parent.ID) ";
  60.     }
  61.     return $join;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top