attachments with tags and get_posts

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!

Read More

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);       

Related posts

Leave a Reply

2 comments

  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) { ///....
    
  2. 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.