wp query - How to restrict add media library only to images of the same post family?

So when I am creating a post, I would like to only be able to see images (after clicking ,add media above wp editor) tha

So when I am creating a post, I would like to only be able to see images (after clicking ,add media above wp editor) that have been uploaded and attached to posts of the same family of the current post i'm creating.

My current code does not seem to work.

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;
} 

So when I am creating a post, I would like to only be able to see images (after clicking ,add media above wp editor) that have been uploaded and attached to posts of the same family of the current post i'm creating.

My current code does not seem to work.

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;
} 
Share Improve this question edited Jul 22, 2019 at 12:13 samjco-com asked Jul 22, 2019 at 5:48 samjco-comsamjco-com 5996 silver badges19 bronze badges 4
  • Can you ellaborate further what you mean by family? Is that a custom taxonomy? Where have you put your code? – Tom J Nowell Commented Jul 22, 2019 at 6:24
  • @TomJNowell Well my post family is Parent/Child/GrandChild.. When I upload any images within the family, I just wanted to show those images as choices when I click add Media. – samjco-com Commented Jul 22, 2019 at 6:27
  • There's only one parameter that ajax_query_attachments_args sends to its callbacks. And what are those code above the add_filter()? They should be in the callback. – Sally CJ Commented Jul 22, 2019 at 10:14
  • this is close to what I am trying to do, but I wanted to add more family posts IDs dynamically: wordpress.stackexchange/questions/156319/… – samjco-com Commented Jul 22, 2019 at 12:09
Add a comment  | 

2 Answers 2

Reset to default 0

You need to alter the query using posts_where and posts_join filters.

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');

Change "WHERE" clause to restrict to specific post type to which selected post belongs to

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;
}

Change "JOIN" clause to get media by restricting to the post parent

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;
}

Got it working!! :) Final Code:

function wpse156319_posts_where( $where, $query ) {

    global $wpdb;

    $currentID = $_POST['post_id']; //shows 2069
    $post = get_post($currentID); //shows 2068
    $parentID = $post->post_parent;
    $chargs = array('post_parent' => $post->ID, 'post_type' => 'projects' );
    $children = get_children($chargs);


    $where .= ' AND (';

    if($children):
        foreach ($children as $child):
            $where .= $wpdb->posts . '.post_parent = '.$child->ID.' OR ';
        endforeach;
    endif;

    if($parentID):
        $where .= $wpdb->posts . '.post_parent = '.$parentID.' OR ';
    endif;

    $where .= $wpdb->posts . '.post_parent = '.$currentID;

    $where .= ')';

    return $where;

}

add_filter( 'ajax_query_attachments_args', 'filterMediaLibrary', 10, 2 );

function filterMediaLibrary($query = array()) {

    add_filter( 'posts_where', 'wpse156319_posts_where', 10, 2 );

    return $query;

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745303660a4621586.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信