最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Query posts only shows 1

programmeradmin1浏览0评论

I have an array of ids:

var_dump($userPostsInternal);
  -> string(13) "128537,128545"

Then I do

$args = array(
    'post__in' => array($userPostsInternal),
    'post_type' => 'post',
    'posts_per_page' => -1,
);
$q = new WP_Query( $args );
foreach ( $q->posts as $post ) {
    $title = $post->title;
    echo $title;
}

But I only get 1 title. There are 2 articles and they do have the ids we see in the var_dump();

I even tried:

foreach ( $q->posts as $post ) {
    $title = get_the_title();
    echo $title;

But I still get one title only.

If I explode $userPostsInternal I get array(2) { [0]=> string(6) "128537" [1]=> string(6) "128545" } and no results at all

I have an array of ids:

var_dump($userPostsInternal);
  -> string(13) "128537,128545"

Then I do

$args = array(
    'post__in' => array($userPostsInternal),
    'post_type' => 'post',
    'posts_per_page' => -1,
);
$q = new WP_Query( $args );
foreach ( $q->posts as $post ) {
    $title = $post->title;
    echo $title;
}

But I only get 1 title. There are 2 articles and they do have the ids we see in the var_dump();

I even tried:

foreach ( $q->posts as $post ) {
    $title = get_the_title();
    echo $title;

But I still get one title only.

If I explode $userPostsInternal I get array(2) { [0]=> string(6) "128537" [1]=> string(6) "128545" } and no results at all

Share Improve this question asked Jul 15, 2019 at 16:48 rob.mrob.m 2072 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

OK this is the thing:

I used a query

$args = array(
    'post__in' => array($userPostsInternal),

Taking the fact the following is a string

var_dump($userPostsInternal);
  -> string(13) "128537,128545"

I thought declaring the array here would work

array($userPostsInternal)

But it's not, therefore thanks to a comment, suggesting me to explode $userPostsInternal converting the string into an array, I then had to remove the array declaration, making the query like:

$args = array(
    'post__in' => $userPostsInternal,

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论