Exclude custom field attachment from gallery
-
Hello everybody, after a couple of days struggling with the code, I’ve decided to shout out for a solution. A custom field attached image keeps showing up in the gallery. I thought it would simply be needed to put the post meta into the ‘exclude’ array, which also passes (and regularly excludes) the post thumbnail, but I was wrong. The $spotlight variable content is nothing but the image url, which isn’t accepted by the array, which seems to only accept numerical IDs. How can I dynamically retrieve the attachment’s numerical ID from the custom field url? Thanks everyone for helping me out.
<?php $thumbnail = get_post_thumbnail_id(); $spotlight = get_post_meta($post->ID, 'spotlight', true); $args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => ASC, 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID, 'exclude' => array($thumbnail,$spotlight) ); $attachments = get_posts($args); foreach ( $attachments as $attachment ): echo '<div>'; echo wp_get_attachment_image($attachment->ID, 'full'); $description = $attachment->post_content; if ($description): echo '<span class="img_desc">' . $description . '</span>'; endif; echo '</div>'; endforeach; ?>
-
Bump
As far as I can see, there is no built-in function to get
meta_idbymeta_keyor any other way. That doesn’t mean you can’t retrieve it manually, such as described in wpseek.com article.Thanks lamosty, in the meantime I’ve made an interesting discover browsing into the database. There’s this meta_key called
_wp_attached_filewhich refers to apost_id 4708which, if inserted into the exclude array, effectively hides the ‘spotlight’ image as desired! I’m feeling I can use the get_attached_file() with filters to solve the problem. What do you think?Yeah, looks like a good solution to me. However, I’m still relatively new to digging in the WordPress Core so I might be wrong. 🙂 Could you copy-paste your code if it is working, so others can see the solution? Thanks.
Solved! The only way to get the attachment numerical id was asking it to the database. Since the get_post_meta output was the image url, it was relatively simple to obtain its id number. I have used this function into the functions.php:
function get_attachment_id_from_src ($image_src) { global $wpdb; $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'"; $id = $wpdb->get_var($query); return $id; }The new function was then called into the exclude array, so that the custom field image has been handled just like a common thumbnail. I hope this will help someone else struggling with custom fields images attachments!
<?php $thumbnail = get_post_thumbnail_id(); $id = get_the_ID(); $meta_key = 'spotlight'; $meta_value = get_post_meta($id, 'spotlight', true); $meta_key_id = get_mid_by_key( $id, $meta_key ); // functions.php $meta_attachment_id = get_attachment_id_from_src ($meta_value); // functions.php $args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => ASC, 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID, 'exclude' => array($thumbnail,$meta_attachment_id) ); $attachments = get_posts($args); foreach ( $attachments as $attachment ): echo '<div>'; echo wp_get_attachment_image($attachment->ID, 'full'); $description = $attachment->post_content; if ($description): echo '<span class="img_desc">' . $description . '</span>'; endif; echo '</div>'; endforeach; ?>
The topic ‘Exclude custom field attachment from gallery’ is closed to new replies.