php 如何将数据动态传递给post__in

np8igboo  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(172)

我的页面上有以下代码

$speakersselected = get_post_meta($post->ID, 'speakersselected', true);
print_r($speakersselected); //getting output (587,585)

        $s_post = array(
          'showposts' => 80, 
          'post_type' => 'speaker',
          'post_status'  => 'publish',
          'post__in'    =>$speakersselected,
           //'post__in'    =>array($speakersselected),
            //'post__in'    => array(587,585),
           );
    
       
     echo"<pre>";
     print_r($s_post);

输出量
如果我使用'post__in' =>$speakersselected,那么我将得到以下输出

Array
(
    [showposts] => 80
    [post_type] => speaker
    [post_status] => publish
    [post__in] => 587,585
)

如果我使用'post__in' =>array($speakersselected),则低于输出

Array
(
    [showposts] => 80
    [post_type] => speaker
    [post_status] => publish
    [post__in] => Array
        (
            [0] => 587,585
        )

)

这两种输出都对我不起作用
如果我添加静态数据'post__in' => array(587,585),那么它对我有效

Array
(
    [showposts] => 80
    [post_type] => speaker
    [post_status] => publish
    [post__in] => Array
        (
            [0] => 587
            [1] => 585
        )

)

知道如何在post__in中发送动态数据吗?

tyg4sfes

tyg4sfes1#

您正在获取$speakersselected中的字符串,而'post__in'属性需要数组格式的值。因此,您需要将字符串转换为数组,然后才能正常工作。

$speakersselected = get_post_meta($post->ID, 'speakersselected', true);
$speakersselected = explode(',',$speakersselected);

    $s_post = array(
      'showposts'   => 80, 
      'post_type'   => 'speaker',
      'post_status' => 'publish',
      'post__in'    => $speakersselected,
    );

相关问题