如何将添加媒体库限制为只添加相同帖子系列的图像?

时间:2019-07-22 作者:samjco

因此,当我创建帖子时,我只希望能够看到上传并附加到与我当前创建的帖子相同系列的帖子上的图像(单击后,在wp编辑器上方添加媒体)。

我当前的代码似乎不起作用。

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

2 个回复
SO网友:Awais

您需要使用posts_whereposts_join 过滤器。

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\');
更改“WHERE”子句以限制所选职位所属的特定职位类型

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;
}
更改“JOIN”子句以通过限制到post父级来获取媒体

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

SO网友:samjco

成功了!!:)最终代码:

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;

}

相关推荐

使用‘MEDIA_HANDLE_UPLOAD’重命名图像文件名

我需要重命名用户在我的网站上发布时上传的图像。我有一个代码,可以完美地保存图像并将其作为帖子的一部分。但我希望这些图片被重命名,在其文件名中包含帖子的标题。我拥有的代码:require_once( ABSPATH . \'wp-admin/includes/image.php\' ); require_once( ABSPATH . \'wp-admin/includes/file.php\' ); require_once( ABSPATH . \'wp-admin/includes/m