• Resolved Grafco

    (@grafco)


    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;
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Grafco

    (@grafco)

    Bump

    As far as I can see, there is no built-in function to get meta_id by meta_key or any other way. That doesn’t mean you can’t retrieve it manually, such as described in wpseek.com article.

    Thread Starter Grafco

    (@grafco)

    Thanks lamosty, in the meantime I’ve made an interesting discover browsing into the database. There’s this meta_key called _wp_attached_file which refers to a post_id 4708 which, 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.

    Thread Starter Grafco

    (@grafco)

    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;
    
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Exclude custom field attachment from gallery’ is closed to new replies.