【发布时间】:2016-03-12 00:18:48
【问题描述】:
我在尝试将 Javascript 变量用作 WordPress 查询参数时遇到了一些问题。
我正在使用 AJAX 发布请求向 Wordpress 发送一个 post_ids 的 Javascript 数组。
$.post('url', { data: requestIds }, function(response) { console.log(response) });
我基本上是在尝试将 'requestIds' 的 Javascript 数组作为 post__in WP_Query 参数传递。
$COMPARISON_QUERY = new WP_Query(array(
'post_type' => array('Post'),
'post__in' => // this is where the array would be passed as a parameter
));
这是处理请求的 PHP:
$response_object = DecodeJSONString($_POST['data']);
function DecodeJSONString($string) {
$decoded_string = json_decode($string);
return $decoded_string; // this would be the query parameter
}
感谢您的任何反馈!
【问题讨论】:
-
那你有什么问题??
-
好吧,为了使用 WP_Query 我需要包括以下内容:define('WP_USE_THEMES', false); require_once('../../../wp-load.php');但是,这样做会破坏 json_decode 函数,使数组的值为“NULL”。这意味着当我尝试将其用作参数时,我只是将 NULL 作为 post__in 传递。
-
请
var_dump($_POST)并编辑您的问题以显示输出 - 可能您发送的是常规键值对,而不是 json -
感谢 Connor - 您能否确认 var_dump 输出是否适用于定义和要求行是否包含在内?您的函数会中断似乎很奇怪 - 但也许已经有一个具有该名称的函数 - 您可以尝试直接调用
json_decode吗? -
永远不要使用
require_once('../../../wp-load.php')这是一个老生常谈的话题,应该贴在某个地方:ottopress.com/2010/dont-include-wp-load-pleaseottopress.com/2010/…
标签: javascript php jquery ajax wordpress