1

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?

6
  • category_name takes the slug of the category name, not the name Oct 29, 2014 at 16:23
  • Yes, you are correct I am using the slug, which I have corrected my question. Oct 29, 2014 at 17:42
  • What does "It won't be attached to a post" mean and please explain "I do have taxonomies associated with images" Oct 29, 2014 at 17:43
  • Just a tip, make double sure that your info in your question is correct before posting. This will eliminate us answering incorrect info Oct 29, 2014 at 17:44
  • Sorry Pieter I will do better next time. I have edited my question and hopefully that makes more sense. Oct 29, 2014 at 19:02

1 Answer 1

1

'category_name' will not work. You are making use of a custom taxonomy, not build-in categories. It seems from this that you have a misunderstanding about taxonomies. Please take your time and see this post on this particular subject

For custom taxonomies, use a tax_query to retrieve the posts that you need. Here is an example:

$query_images_args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'attachment_category',
            'field'    => 'slug',
            'terms'    => 'SLUG OF YOUR TERM',
        ),
    ),
);
$query_images = new WP_Query( $query_images_args );
2
  • Oh... Ok, yes I will look over that post. Thank you for your time! Oct 29, 2014 at 19:17
  • Yes, thank you Pieter. I just clicked the check mark as the accepted answer. Oct 30, 2014 at 17:07

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.