我正在尝试为WordPress REST API创建自定义端点,并通过URL传递参数。
当前端点是:
/wp-json/v1/products/81838240219
我想要实现的是一个看起来像这样的端点,并且能够在回调中检索identifier参数。
/wp-json/v1/products?identifier=81838240219
// Custom api endpoint test
function my_awesome_func( $data ) {
$identifier = get_query_var( 'identifier' );
return $identifier;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/products=(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
Run Code Online (Sandbox Code Playgroud) 标题说明了一切。我知道评论是 Wordpress 中的原生评论帖子类型。我已经包含了添加评论的代码。
然而,问题是我不清楚如何给评论评级以及如何将其与特定产品联系起来。当我使用 comment_post_ID 时,它似乎没有将评论(评论)分配给正确的帖子。
$time = current_time('mysql');
$data = array(
'comment_post_ID' => 1,
'comment_author' => 'admin',
'comment_author_email' => 'admin@admin.com',
'comment_author_url' => 'http://',
'comment_content' => 'content here',
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 1,
'comment_author_IP' => '127.0.0.1',
'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)',
'comment_date' => $time,
'comment_approved' => 1,
);
wp_insert_comment($data);
Run Code Online (Sandbox Code Playgroud)