What I am needing to do here is get an image from the media library only by it's category. It won't be attached to a post, meaning that, the image will not be uploaded as a "featured image" of any post type. It will be uploaded directly through the media library. However I have created a function that will allow me to apply custom categories to those images. I have been successful at pulling images from my media library with the following:
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
}
And this is my custom function for categories:
function add_attachment_taxonomy() {
$labels = array(
'name' => 'Categories',
'singular_name' => 'Category',
'search_items' => 'Search Category',
'all_items' => 'All Category',
'parent_item' => 'Parent Category',
'parent_item_colon' => 'Parent Category:',
'edit_item' => 'Edit Category',
'update_item' => 'Update Category',
'add_new_item' => 'Add New Category',
'new_item_name' => 'New Category Name',
'menu_name' => 'Categories',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'query_var' => 'true',
'rewrite' => 'true',
'show_admin_column' => 'true',
);
register_taxonomy( 'attachment_category', 'attachment', $args );
}
add_action( 'init', 'add_attachment_taxonomy' );
However when I add 'category_name' => term_slug
it continues to show nothing. Is there something else I need to be doing to make this work?
category_name
takes the slug of the category name, not the name