0

First line of the code below carries post ids captured via a form. Then rest of the code helps pull the post-thumbnail of the respective post ids

$sel_ids = $_SESSION['selected_ids'];
if ( ! is_array( $sel_ids ) ) { //the check to prove that $sel_ids is a valid array
return;
}

require_once('../../../../wp-load.php');

$args = array( 'post__in' => $sel_ids, 'post_type' => 'post' );
$posts = get_posts($args);

foreach ($posts as $post) {
    echo get_the_post_thumbnail($post->ID, 'coverimage');
}

enter image description here

Thumbnails display fine but before the thumbnails, the code also outputs a word Array which is definitely unwanted.


I have tried commenting out the lines one by one to isolate what line exactly is the culprit. So I found that this line $args = array( 'post__in' => $sel_ids, 'post_type' => 'post' ); generates the unwanted string Array. My guess is the var $sel_ids needs some work.

FYI a var_dump($sel_ids) prints array(2) { [0]=> string(2) "67" [1]=> string(2) "16" }. I also tried the code explode(',', $sel_ids); from the answer here but it prints a warning Warning: explode() expects parameter 2 to be string. Which I think makes sense because I know that $sel_ids is an array, but then why is it printing the word Array, odd.


FYI Since the code is a part of an html email. Here is a relevant part of the source of the email in gmail. Just in case it helps to infer the issue better.

Array<img src="http://xxxxxxxxx.com/wp-content/uploads/2014/04/360.jpg"> 

UPDATE : Test code <pre><?php print_r($args);?></pre> outputs the results below. I suspect the word Array is being printed out from [post__in] => Array. How do I remove or replace this.

Array
(
    [post__in] => Array
        (
            [0] => 67
            [1] => 16
        )

    [post_type] => post
)
2
  • 1
    Your code looks fine. Are you sure Array is not printing from somewhere else? Not relevant but you should use the_post_thumbnail() when echo the html. Also there should be if(!empty($posts)) check before try to loop through $posts.
    – Sisir
    May 23, 2014 at 6:49
  • @Sisir if(!empty($posts)) does not make any difference. In fact any line after $args = array( 'post__in'....... do not print anything, I have seen so by commenting all other lines. the_post_thumbnail() is irrelevant as I need to grab the image url by post ID.
    – gurung
    May 23, 2014 at 7:08

1 Answer 1

0

Array is how PHP represents an array when it is converted to a string:

echo array( /* anything in here */ ); // output: Array

Fortunately for you, PHP doesn't really like converting arrays to strings, so it will give an error like this:

PHP Notice: Array to string conversion in /somefile.php on line 345

Usually these PHP notices are silenced by default. To see them, you should enable WordPress debugging mode with WP_DEBUG. Frankly, you should have WP_DEBUG enabled any time you are developing something with WordPress :-).

Once you've enabled debugging mode, run your code and check the debug.log. You should see an array to string conversion notice from PHP, which will tell you where this came from.

Like @Sisir, I don't see any problems with your code. I suspect that the array is being output by a function hooked to one of the actions or filters called by get_the_post_thumbnail(): 'post_thumbnail_size', 'begin_fetch_post_thumbnail_html', 'end_fetch_post_thumbnail_html', and 'post_thumbnail_html'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.