Forum Replies Created

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

    Thread Starter Grafco

    (@grafco)

    Bump

Viewing 3 replies - 1 through 3 (of 3 total)