大家好,我有一些来自外部公司的关于我的WP的数据,这些数据位于/WP json/WP/v2/jobs/将作为帖子导入。
此数据包含需要导入的JSON,但它试图使用字符串(例如“Manager”)而不是术语\\u id(即“381”)来设置分类法。这意味着我返回了一个错误。
{
"code": "rest_invalid_param",
"message": "Invalid parameter(s): job_location, job_industry, job_sector",
"data": {
"status": 400,
"params": {
"job_location": "job_location[0] is not of type integer.",
"job_industry": "job_industry[0] is not of type integer.",
"job_sector": "job_sector[0] is not of type integer."
}
}
}
所以我想要的是当他们将此值发布给我们时,例如:“job\\u sector”:“Manager”
相反,我希望遍历我们的分类法,并找到这个“Manager”job\\u扇区的ID,重建JSON和THEN 让WP导入我传递的数据,使用正确的ID无错误。
有谁能帮助我拦截JSON并以正确的格式传递它吗?
我尝试了“rest\\u pre\\u dispatch”,但这似乎只是编辑发送回他们的结果,WP已经对其进行了处理。
编辑:这是我的代码:
function wpse281916_rest_check_referer( $result, $server, $request ) {
if ( null !== $result ) {
// Core starts with a null value.
// If it is no longer null, another callback has claimed this request.
// Up to you how to handle - for this example we will just return early.
return $result;
}
$array = json_decode($result, true);
$term = get_term_by(\'name\',$array["job_sector"],\'job_sector\');
$term = json_decode(json_encode($term),true);
$termid = $term[\'term_id\'];
$array[\'job_sector\'] = $termid;
$result = json_encode($array);
return $result;
}
// add the filter
add_filter( \'rest_pre_dispatch\', \'wpse281916_rest_check_referer\', 10, 3 );
编辑2:建议后 function wpse281916_rest_check_referer( $result, $server, $request ) {
if ( null !== $result ) {
// Core starts with a null value.
// If it is no longer null, another callback has claimed this request.
// Up to you how to handle - for this example we will just return early.
return $result;
}
$array = json_decode($request, true);
$term = get_term_by(\'name\',$array["job_sector"],\'job_sector\');
$term = json_decode(json_encode($term),true);
$termid = $term[\'term_id\'];
$array[\'job_sector\'] = $termid;
$request = json_encode($array);
return null;
}
// add the filter
add_filter( \'rest_pre_dispatch\', \'wpse281916_rest_check_referer\', 10, 3 );