2

I have a WP blog with a few pages and posts and several categories. I want to write a loop to retrieve images attached to the posts (not the pages) and only those from a specific category and it's children. The category id is 60.

This is the WP_Query call:

$my_query = new WP_Query( array(
  'post_type' => 'attachment',
  'cat' => '60',
  'post_mime_type' =>'image',
  'posts_per_page' => $batch_size,
  'nopaging' => false,
  'post_status' => 'all',
  'post_parent' => null,
  'meta_key' => 'my_hash',
  'orderby' => 'meta_value'
) );

But I get nothing!

What am I doing wrong?

3 Answers 3

1

You'll need to first grab the posts, then grab the attachments that are children of said posts.

$my_query = new WP_Query( array(
    'meta_key' => 'my_hash',
    'nopaging' => true,
    'orderby' => 'meta_value',
    'fields' => 'ids',
    'cat' => '60',
));

if ( $post_ids = $my_query->get_posts() ) {
    $post_ids = implode( ',', $post_ids );
    $atts_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_parent IN($post_ids) AND post_type = 'attachment'" );   

    $my_query->query( array(
        'posts_per_page' => $batch_size,
        'post_mime_type' =>'image',
        'post_status' => 'all',
        'post_type' => 'attachment',
        'post__in' => $atts_ids,
    ));
}
3
  • I tried making this change to this Image Wall plugin, but I was unable to get any success. Could you have a go for me? Thanks.
    – aalaap
    Jun 27, 2012 at 11:01
  • This plugin currently displays a wall with images from all posts, pages etc. on the blog. I want to restrict it to posts from a particular category or categories.
    – aalaap
    Jun 27, 2012 at 11:08
  • I asked the developer of the plugin directly and he liked the idea, so he's looking into it! :-)
    – aalaap
    Jun 28, 2012 at 10:18
1

Attachment posts don't have categories, their parents 'post' posts do, so you have a two step problem.

  • Find the IDs of the posts of type post in the category ( 60? )
  • Find the attachments that have a post_parent that is in the list we found in our first query
1
  • Thanks - I was going to try this out in code, but @TheDeadMedic already did it for me!
    – aalaap
    Jun 27, 2012 at 7:49
0

If you want to get image links frop category and subcategories you should use this:

    global $wpdb;
$id = 60; // your cat id
$myrows = $wpdb->get_results( "SELECT p2.ID, p2.guid ".
    "FROM $wpdb->posts as p1, $wpdb->posts as p2, $wpdb->term_relationships as tr, $wpdb->term_taxonomy as tt  ".
    "WHERE ".
    "((tr.term_taxonomy_id = $id AND ".
    "p1.ID = tr.object_id) OR ".
    "(tt.parent = $id AND ".
    "tr.term_taxonomy_id = tt.term_taxonomy_id AND ".
    "p1.ID = tr.object_id)) AND ".
    "p1.post_status = 'publish' AND ".
    "p2.post_parent = p1.ID AND ".
    "p2.post_mime_type LIKE 'image%'".
    "GROUP BY p2.ID".
    "<= 5". // you able to limit the db answer
    "", ARRAY_A );
print_r( $myrows );

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.