0

Basically i need help to build a proper array to pass as value of post__in

Basically code has to get featured images of posts only for selected ids. I need your help to extract the values of the concerned array string in the right manner as currently the code gives a warning Warning: implode(): Invalid arguments passed in .../... It still displays images though. The issue is it displays images from across all posts instead of images only from posts with selected ids, as intended.

 $sel_ids = $_SESSION['selected_ids'];

 global $wpdb; 
 require_once('../../../../wp-load.php');

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

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

In the first line of code block above, this string $sel_ids carries ids of selected posts. When echoed gives Array. When var_dump($sel_ids) shows

array(2) { [0]=> string(2) "67" [1]=> string(2) "16" }

and foreach gives a string such as 6716. I do not need the foreach in my code but giving additional info so that it may help you infer better. The code above is not on a template page, but instead on a separate page. it is actually a form-processor. I need the code to run here as I need to grab those values and email them later. So I have loaded WordPress globals. The session is there to carry array value from the form.


UPDATE - @Mamaduka - Here is a relevant part of the source of the email in gmail

Array<img src="http://xxxxxxxxx.com/wp-content/uploads/2014/04/360.jpg">
5
  • "The code below is not on a template page, but instead on a separate page" please explain what is separate page. And why are you using sessions. May 22, 2014 at 6:49
  • 1
    Well, args isn't a proper variable, you're missing "$". You can also try creating a variable to hold the imploded string for later use, rather than trying to use it in the array itself. ie. $post_string = implode(', ', $sel_ids');
    – Ian
    May 22, 2014 at 6:59
  • You should check with is_array() before implode. Also use has_post_thumbnail() in condition before try to echo post thumbnail. And fix args to $args :)
    – Sisir
    May 22, 2014 at 7:06
  • post__in should receive an array, not a string.
    – Milo
    May 22, 2014 at 7:26
  • What happens when you just echo $post_string?
    – Ian
    May 22, 2014 at 18:24

1 Answer 1

1

Problem here is that, post_in accepts only array of post IDs and implode() returns string.

Here's how fixed code should look like (also added few conditional checks):

$sel_ids = $_SESSION['selected_ids'];

/**
 * Check, if we have array of Post IDs
 */
if ( ! is_array( $sel_ids ) ) {
    return;
}

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

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

if ( empty( $posts ) ) {
    return;
}

foreach ($posts as $post) {
    echo get_the_post_thumbnail($post->ID, 'coverimage');
}
5
  • That is correct, looks like I jumped through hoops for nothing. Thanks.
    – gurung
    May 22, 2014 at 9:38
  • Although it works now, this line $args = array( 'post__in' => $sel_ids, 'post_type' => 'post' ); seems to html output the word Array which is odd. Any ideas to avoid that.
    – gurung
    May 22, 2014 at 20:12
  • @gurung can you link the screenshot here, source code of the HTML output would be best.
    – Mamaduka
    May 23, 2014 at 7:27
  • The output is a part of an email so I grabbed this from the source of the email in gmail.Please look at the UPDATE in the bottom of the question. Also I have asked this as a new question.
    – gurung
    May 23, 2014 at 7:38
  • Also I have asked in a new question here. If possible please answer there.
    – gurung
    May 23, 2014 at 7:43

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.