1

I registered a custom taxonomy for ATTACHMENTS. Everything is fine on the admin side : I can tag medias with my custom taxonomy's categories, they show up with my images, etc.

I then build a template file for an Album page which filters images that have been tagged with my custom taxo's categories (grouped by categories).

Here's a sample of my code for this page :

$post_type = 'attachment';
$tax = 'albums';
$tax_terms = get_terms($tax,array('orderby' => 'id', 'order' => 'DESC') );
        if ($tax_terms) {
          foreach ($tax_terms  as $tax_term) {
              $args=array(
              'post_type' => $post_type,
              'albums' => $tax_term->slug,
              'post_status' => 'publish',
              'posts_per_page' => -1,
              'caller_get_posts'=> 1
              );

              $my_query = null;
              $my_query = new WP_Query($args);
              if( $my_query->have_posts() ) {
                $terme = $tax_term->slug;
                $lien_album = 'http://www.example-site.com/wp/section/albums-photo/album-individuel/?album='.$terme;
                echo '<a href="'.$lien_album.'"><h3 style="margin-bottom:-8px;">'.$tax_term->name.'</h3></a><br/>';
                while ($my_query->have_posts()) : $my_query->the_post(); 
                  $image_id = get_the_ID();
                  $image_adresse = wp_get_attachment_image_src( $image_id, 'medium');

                  ?>
                  <div class="vignettes">
                  <img src="<?php echo $image_adresse[0]; ?>" class="vignette" alt="<?php the_title_attribute(); ?>" />
                  </div>

                  <?php
                endwhile;
              }
              wp_reset_query();
              ?>
              <a class="bouton-albums" href="<?php echo $lien_album; ?>">Voir les photos de cet album</a>
              <p style="clear:both;">&nbsp;</p>
              <?php
          }
        }               

I noticed that only images that were used in an actual page (or post) showed up in my template page.

After analysing the Codex's register_taxonomy function, I read about an argument called 'update_count_callback' and that it should use the value '_update_generic_term_count' when using a custom taxo for something other than posts. I then added it in my register_taxonomy function arguments as follow:

'update_count_callback' => '_update_generic_term_count',

Unfortunately, that doesn't seem to work. Or maybe this argument has nothing to do with what I'm trying to achieve?

I don't want each of my Albums' images to be in a page or post. I simply want to be able to add some images in the media library, tag them with my custom taxo and make them show up in my Album page (which uses my special template).

I hope someone will be able to tell me where I'm wrong or how to get what I want!

0

When you query attachments, you have to take care about the post_status argument that have a different meaning than for other post types. Attachments have the status of inherit which means that there status should be the same as their parent post (ie the post from where they have been uploaded). This makes attachments associated to not published posts, and attachments that have been uploaded directly to the Media Library missing.

So to make you code work, you could replace

'post_status' => 'publish',

with

'post_status' => 'any',

Regarding the update_count_callback parameter, it has to do with the count numbers that are displayed at some place in the admin. You can use this callback to compute this number yourself. So it has nothing to do with your issue.

  • Well, that just solved my problem! Everything's working fine now. Thanks a lot, I'd been working on this issue for hours! – Petit chemin Jan 19 '16 at 2:41

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.