自定义帖子类型特定媒体 - WordPress

时间:2016-11-18 14:02:42

标签: php wordpress

我在WordPress中创建了一个自定义帖子类型,但想知道是否可以指定特定于自定义帖子类型的上传目录,以及在进行和插入媒体时媒体库仅显示上传到特定目录中的媒体自定义帖子类型。

保存图像和媒体库仅在以下位置显示媒体:

/wp-content/uplodas/custom_post_type/

更新

我有以下代码,它将过滤我正在寻找的内容,但我只想在client_gallery的CPT上运行过滤器。我的if语句似乎不起作用?

function wpse48677_get_cpt()
{

    if ($_GET['post_type'] == "") {
        $posts = $_GET['post'];
        $postType = get_post_type($posts);
    } else {
        $postType = $_GET['post_type'];
    }

    return $postType;

}



    function my_posts_where($where)
    {
        global $wpdb;

        $post_id = false;
        if (isset($_POST['post_id'])) {
            $post_id = $_POST['post_id'];

            $post = get_post($post_id);
            if ($post) {
                $where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
            }
        }

        return $where;
    }

    function my_posts_join($join)
    {
        global $wpdb;



    $join .= " LEFT JOIN {$wpdb->posts} as my_post_parent ON ({$wpdb->posts}.post_parent = 'client_gallery') ";

        return $join;
    }


    function my_bind_media_uploader_special_filters($query)
    {


            add_filter('posts_where', 'my_posts_where');
            add_filter('posts_join', 'my_posts_join');


        return $query;
    }


print wpse48677_get_cpt();
if(wpse48677_get_cpt() == 'client_gallery') {
// THIS IF STATEMENT DOESNT SEEM TO WORK? I KNOW  $doJoin == client_gallery
// AS I CAN SEE IT IF I PRINT IT OUT (see above)
// IF I RUN THE ADD FILTER WITHOUT THE IF I GET THE RESULTS I"M LOOKING FOR?????
    add_filter('ajax_query_attachments_args', 'my_bind_media_uploader_special_filters');
}

1 个答案:

答案 0 :(得分:2)

如果您在自定义帖子类型声明发生的地方添加此代码,您将为自定义帖子类型制作上传目录。每次上传发生在帖子类型为您的帖子时,也会使用此文件夹。

function super_upload_dir( $args ) {
    if(!isset($_REQUEST['post_id']) || get_post_type( $id ) != $YOUR_POST_TYPE) 
        return $args; 

    $id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );

    if( $id ) {    
       $newdir = '/' . $DIRECTORY_NAME;

       $args['path']    = str_replace( $args['subdir'], '', $args['path'] ); 
       $args['url']     = str_replace( $args['subdir'], '', $args['url'] );      
       $args['subdir']  = $newdir;
       $args['path']   .= $newdir; 
       $args['url']    .= $newdir; 
    }
    return $args;
}
add_filter( 'upload_dir', 'super_upload_dir' );

对于librairy过滤器,我已经使用了过去的插件,它有一种基于短代码过滤媒体项目的方法,但我不知道该怎么做才能根据后端的当前帖子类型对其进行过滤。遗憾