1

I am attempting to resolve a problem I continue to have with the media library.

Essentially, what I am looking to do is "automatically" assign media which is uploaded from a custom post type "edit post" page to a predefined category.

Based on other answers on this site I have found various people discussing solution (one of these links is below) which allow you to "manually" assign a category to media library elements. My aim however is to somehow define specific post types and the associating category which should be applied.

At the same time I would also like the ability to restricting a "specific post type" so when the media library lightbox is requested from the edit post page that the "media library" ONLY shows items within the categories I define.

I know this might sound a bit confusing so let me provide a quick example.

Let's say I have a custom post type called "employee news". Now let's say that someone proceeds to add/edit a post within this custom post type. Let's further assume that he now uses the editor to add content and now want to add one/more images. As expected he would select the "add media" button above the editor and either (a) upload a new image or (b) select an image from the media library. THESE are the two areas I am talking about.

What I need is the correct code to put into my functions.php file so that either one or both of the following can be set individually:

(a) Ability to specify a custom post type and define the hidden category name to be associated with any file uploaded to the media library.

(b) Ability to specify a custom post type and define the "restricted categories" to be used when presenting the files available for the user to select from.

How can this be achieved correct?

Related Question related to Category assignment of media elements: Can I add a Category Metabox to attachment?

Related Question related to conditional logic related to the media library: Conditional add_filter?

1
  • You can run a pre_get_posts or parse_query filter for the upload window just like you can for any admin page(i think), just check $pagenow inside the callback and make sure it's media-upload.php..and re-set applicable query vars as needed.
    – t31os
    Feb 19, 2011 at 15:13

1 Answer 1

1

I think you can go about the filter list_terms_exclusions, but i don`t tested.

example:

function fb_list_terms_exclusions($args) {

    if ( 'media-upload-popup' != $where )
        return $where;

    if ( !current_user_can('manage_categories') )
        $where = " AND t.slug  NOT IN ('uncategorized', 'category-2')"; // slug of categories

    return $where;
}
add_filter( 'list_terms_exclusions', 'fb_list_terms_exclusions' );

update for the comments to this asnwer: an example to find the post types; at first a small example to use the default variables and after this an funtion for return an value:

/**
 * Include my scripts
 *
 * $pagehook for check, if this the right page
 * $post_type for check, if this the right post type
 */
function fb_post_type_script($pagehook) {
    global $post_type;

    $pages = array( 'edit.php', 'post.php', 'post-new.php' );
    if ( in_array( $pagehook, $pages ) && $post_type == 'my_post_type' ) {
        wp_enqueue_script( 'my_script_example_key', plugins_url( 'js/my_example_script.js', __FILE__ ), array( 'jquery', 'my_other_example_script_key' ), '1.0.0' );
    }
}
add_action( 'admin_enqueue_scripts', 'fb_post_type_script' );

alternative you use a custom function to find the post type:

/**
 * Return post type
 *
 * @return string $post_type
 */
private function get_post_type() {

    if ( !function_exists('get_post_type_object') )
        return NULL;

    if ( isset($_GET['post']) )
        $post_id = (int) $_GET['post'];
    elseif ( isset($_POST['post_ID']) )
        $post_id = (int) $_POST['post_ID'];
    else
        $post_id = 0;

    $post = NULL;
    $post_type_object = NULL;
    $post_type = NULL;
    if ( $post_id ) {
        $post = get_post($post_id);
        if ( $post ) {
        $post_type_object = get_post_type_object($post->post_type);
            if ( $post_type_object ) {
                $post_type = $post->post_type;
                $current_screen->post_type = $post->post_type;
                $current_screen->id = $current_screen->post_type;
            }
        }
    } elseif ( isset($_POST['post_type']) ) {
        $post_type_object = get_post_type_object($_POST['post_type']);
        if ( $post_type_object ) {
            $post_type = $post_type_object->name;
            $current_screen->post_type = $post_type;
            $current_screen->id = $current_screen->post_type;
        }
    } elseif ( isset($_SERVER['QUERY_STRING']) ) {
        $post_type = esc_attr( $_SERVER['QUERY_STRING'] );
        $post_type = str_replace( 'post_type=', '', $post_type );
    }

    return $post_type;
}

public function example() {
    $post_type = $this->get_post_type();  

    // FB_POST_TYPE_1 is defined with my post type
    if ( FB_POST_TYPE_1 == $post_type ) {
        // tue etwas, wenn wahr
    }
}
2
  • I will give this a try but how should this work for different post types? In other words, can you please extend this to show how only specific categories are shown when your on different custom post types? Thanks in advance Feb 21, 2011 at 0:22
  • @NetConstructor.com: i have update the answer, i hope its that, waht you search. You can also user the function get_query_var(): $post_type = 'my_example_post_type'; if ( get_query_var('post_type') === $post_type ) { // tue was, wenn wahr }
    – bueltge
    Feb 21, 2011 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.