1

having problems with filtering a posts attachments by tag... It worked fine until i added the tag__in arg (and other variations of it).

$vidArgs = array(
                            'tag__in' =>5,
                            'post_type' => 'attachment',
                            'post_parent' => $post->ID,
                            'post_mime_type'=>'video/quicktime',
                            'posts_per_page'=>20
                        );

$videos = get_posts($vidArgs);

foreach ($videos as $vid) { ///....

pointers / advice appreciated!

UPDATES it is odd. It just doesn't work with media tags! Added testtag to a post and it comes up. There must be a way to get attachments by tags? Otherwise what's the point? The functionality is in wp admin from the Media Tags pane...

$argsc = array(
    /* 'tag' => 'commercials,testtag',  */
    'tag__in' => array(5,11),
    'post_type'=>array('post','page','attachment'),
    'post_status'=>'any',
    /* 'post_parent'=>$post->ID, */
    'posts_per_page'=>20
);

$the_queryB = get_posts( $argsc);

echo count($the_queryB).", <pre>";
print_r($the_queryB);       
  • WordPress tags aren't associated with attachments natively, i don't think(assume) the WP_Query class supports querying attachments by taxonomy(because it's not a natively supported feature, despite the fact it can be enabled). Try the same query minus the mime and using the post post type, if results come back fine then it confirms what i've said. – t31os Aug 5 '11 at 9:35
  • Also note, get_posts defaults to publish post_status, if you're querying attachments you need to explicitly set the post_status parameter. – t31os Aug 5 '11 at 9:36
  • thanks t3 - figured out the post_status yesterday which helped but the tags element still doesn't work. Can this function really not exist?! – v3nt Aug 5 '11 at 10:33
  • My point is i don't think WP_Query was written with any expectation for attachments to have taxonomies, and therefore may not work as a result(i could be wrong of course). – t31os Aug 5 '11 at 10:37
1

From this page in the codex, i see that this parameter requires an array so i'd try this way:

$vidArgs = array(
                            'tag__in' => array(5),
                            'post_type' => 'attachment',
                            'post_parent' => $post->ID,
                            'post_mime_type'=>'video/quicktime',
                            'posts_per_page'=>20
                        );

$videos = get_posts($vidArgs);

foreach ($videos as $vid) { ///....
  • thanks kevin but tried that earlier & doesn't work... – v3nt Aug 4 '11 at 14:39
  • What if you use the tag_id instead? – kevin Aug 4 '11 at 14:44
  • afraid not! Seems odd as thought this would be fairly straight forward..;-/ – v3nt Aug 4 '11 at 14:48
  • have you tryed all others parameters? Stupid question, are you sure there is a tag with the id you require and a post that matches those arguments? – kevin Aug 4 '11 at 15:03
  • yeh - really annoying. maybe something to do with attachments? – v3nt Aug 4 '11 at 15:45
1

found n easy way of doing this now! Getting images by 'your case' tag and logged in user.

$args = array( 
'post_type' => 'attachment', 
'author' => $current_user->ID,
'post_status' => 'inherit',
'tax_query' => array(
            array(
                'taxonomy' => 'media_tag',
                'terms' => 'yourcase',
                'field' => 'slug',
            )
        )
);

$attachments = get_posts($args);

UPDATE. media_tag seems to be a custom taxonomy added by the file gallery plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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