Javascript 排除Wordpress查询中带有外部数组的帖子

Javascript 排除Wordpress查询中带有外部数组的帖子,javascript,php,jquery,arrays,wordpress,Javascript,Php,Jquery,Arrays,Wordpress,我试图通过AJAX将数组传递给外部php文件。这是要从查询中排除的帖子ID的硬编码列表 这就是我在数组中设置post ID的地方,这些ID存在于数据库中。然后,我将变量$postsNotIn作为内爆数组传递给JS文件,以传递给ajax.php 问题是,它不起作用。我不认为$postNotIn是作为数组传递的,因为post\u not\u in需要一个数组 我试着传递$features,但也不起作用 我一直得到0作为回应 但是,当我在查询“post\u not\u in'=>数组(23028026

我试图通过AJAX将数组传递给外部php文件。这是要从查询中排除的帖子ID的硬编码列表

这就是我在数组中设置post ID的地方,这些ID存在于数据库中。然后,我将变量
$postsNotIn
作为内爆数组传递给JS文件,以传递给
ajax.php

问题是,它不起作用。我不认为
$postNotIn
是作为数组传递的,因为
post\u not\u in
需要一个数组

我试着传递
$features
,但也不起作用

我一直得到
0
作为回应

但是,当我在查询
“post\u not\u in'=>数组(230280268)中硬编码数组时工作正常

如何将正确数组形式的ID传递到
ajax.php

编辑如果我将
$exclude
包装在
array()
中,就像这样
'post\uu not\u in'=>数组($exclude)
它只会排除第一个post ID,而忽略其余的2个

HTML按钮

$features = array(320, 280, 268); // Array of posts to exclude
$postsNotIn = implode(", ", $features);

<a class="btn btn-lg btn-default load-more-button" 
    data-exclude="<?php echo $postsNotIn; ?>" 
    data-page="1" 
    data-url="<?php echo admin_url('admin-ajax.php'); ?>">
    Load More
</a>
这是外部
ajax.php
文件

function load_more() {
    $paged = $_POST['page'] + 1;
    $exclude = $_POST['exclude'];

    $query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'paged' => $paged,
        'post__not_in' => $exclude
    ));

if($query->have_posts()) :
    while($query->have_posts()) : $query->the_post();
        get_template_part( 'content', get_post_format() );
    endwhile;
else :
    echo 0;
endif;

wp_reset_postdata();

die();

post\u not\u in
需要一个ID数组,而您传递的是逗号分隔的字符串

在将数组转换为字符串以用于属性时,需要反转运行的过程

// Before using the $exclude variable, convert the string to an array
$exclude = array_map( 'trim', explode( ',', $exclude ) );
在评论部分的讨论之后,我删除了分隔符中的空格。AJAX回调接收的字符串没有任何空格


我在数组中的所有元素上使用
trim()
来解释任何有空格的ID。

这并没有解决它。它只排除数组中的第一个ID。输出
$exclude
的值,然后查看数组的实际外观。它应该镜像原始数组,但在尝试进一步调试之前值得确认您提供的新
$exclude
的输出是
数组(size=1)0=>字符串“320280268262”(长度=15)
答案已更新。奇怪的是,在内爆函数(
“,”
)的逗号后面添加的空格没有出现在到达AJAX回调的字符串中。在更新后的答案中,我仅在逗号处内爆,并在空格处修剪您使用的
,“
内爆,因此HTML中的字符串应为“320280268”。正在接收的字符串没有任何空格,因此
explode()
中的原始分隔符不起作用。整个过程变成了一个数组元素。
// Before using the $exclude variable, convert the string to an array
$exclude = array_map( 'trim', explode( ',', $exclude ) );