0

I want to display 3 specifics posts.

Problem : My posts IDs are from a previous array.

Result : It displays only the first one.

Function :

foreach($fav_author_list as $i => $item) {
  $insert = get_user_favorites($item);
  if (!is_array($insert[0])) {
    $result = array_merge($result, $insert);
  }
}
$algoid = implode(",", $result); 

Result from $algoid (Post ID) = 865, 866, 877

I want to display the three posts.

$myarray = array($algoid);
$args = array(
   'post__in'      => $myarray,
);
// The Query
$the_query = new WP_Query( $args );

1 Answer 1

2

You don't have to implode your $algoid for the post__in. Since you're using implode, you're actually passing an array with a string for your query:

array('865, 866, 877'); // Items: 1

However, WP_Query is expecting an array with the ids, not as a string:

array(865, 866, 877); // Items: 3

Here's how it should be:

// Use your function to generate the array with the IDs
$algoid = array(865, 866, 877); 

$args = array(
    'post__in' => $algoid
);

For more information about WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query

post__in (array) - use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.

Sign up to request clarification or add additional context in comments.

1 Comment

Working ! Thank you

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.