I have a portfolio where for each item i have some images inside the content, a featured image and a custom filed called "portfolio images".

I want to get all images by post excluding the fetured image and content images.

The code i have is that:

        if (have_posts()) : while (have_posts()) : the_post();  
        $terms = wp_get_post_terms(get_the_ID(), 'portfolio-type');
        /*get the images for the slide on hover by metelidrissi*/
            $args = array(
                'post_type' => 'attachment',
                'numberposts' => -1,
                'post_status' => null,
                'post_parent' => get_the_ID()

            $attachments = get_posts( $args );
            $attachments_portfolio = array();
            if ( $attachments ) {
                $i = 0;
                foreach ( $attachments as $attachment ) {
                  $attachments_portfolio[$i] = $attachment->guid;

And I only want to take the images ont his field:

enter image description here

What i have to do? There is a wat to exclude the images are on the content post and featured one?



I have solved. The solution is related with the function get_post_meta();

As WordPress as made by the abstraction of differnet kind of posts with the same structure we can define different post types.

Here we can find more information about the function:


And as you can read, you can find 3 vars $post_id, $key and $single.

get_post_meta( int $post_id, string $key = '', bool $single = false )

The second one, with the $key we can indicate wich post we want to retrive, for default it return data for all keys.

In our case we have a template wiht a portfolio with the key *_swimmingpool_image_gallery** and thats the "key" of our problem.

The full code is here:

  $image_gallery = get_post_meta( get_the_ID(), '_swimmingpool_image_gallery', true );
                if(!empty($image_gallery)) {
                    $attachments = array_filter( explode( ',', $image_gallery ) );
                    $aImage = array();
                    if ($attachments) {
                        $i = 0;
                        foreach ($attachments as $attachment) {
                            $attachment_url = wp_get_attachment_url($attachment , 'full');
                            $image = swimmingpool_resize($attachment_url, $slidewidth, $slideheight, true);
                            if(empty($image)) {$image = $attachment_url;}
                            $aImage[$i] = $image;

I wish it can help you if you are in the same issue.

| improve this answer | |

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.