如何使用WP REST API(v1或v2)获取特定自定义帖子类型的所有帖子?我对此很新,并试图了解如何做到这一点.
我目前正在使用WP REST API v2并设法使用此方法获取所有帖子类型的列表
http://domain.com/wp-json/wp/v2/types
Run Code Online (Sandbox Code Playgroud)
然后设法得到我感兴趣的帖子类型
http://domain.com/wp-json/wp/v2/types/the-icons-update
Run Code Online (Sandbox Code Playgroud)
如何获取该特定内容类型的所有帖子?
我试过了
http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update
Run Code Online (Sandbox Code Playgroud)
但它返回一个空数组(我想它会返回默认帖子,在我的网站上只有我试图检索的自定义帖子类型中的帖子).
我是如何注册帖子类型的?
function custom_post_type() {
$labels = array(
'name' => _x( 'The Icons Update', 'post type general name' ),
'singular_name' => _x( 'The Icons Update', 'post type singular name' ),
'add_new' => _x( 'Add Page', 'magazine' ),
'add_new_item' => __( 'Add New Page' ),
'edit_item' => __( 'Edit Page' ),
'new_item' => __( 'New Page' ),
'all_items' => __( 'All Pages' ),
'view_item' => __( 'View Page' …Run Code Online (Sandbox Code Playgroud) 我想这样做而不使用任何类型的插件,因为这些都是核心wordpress功能(自定义字段和REST API).以下是自定义字段的文档供参考:
https://codex.wordpress.org/Using_Custom_Fields
这是我的wordpress安装的截图:
以下是目前帖子的API响应:
{
"_links": {
"about": [
{
"href": "http://example.com/wp-json/wp/v2/types/post"
}
],
"author": [
{
"embeddable": true,
"href": "http://example.com/wp-json/wp/v2/users/1"
}
],
"collection": [
{
"href": "http://example.com/wp-json/wp/v2/posts"
}
],
"curies": [
{
"href": "https://api.w.org/{rel}",
"name": "wp",
"templated": true
}
],
"replies": [
{
"embeddable": true,
"href": "http://example.com/wp-json/wp/v2/comments?post=21"
}
],
"self": [
{
"href": "http://example.com/wp-json/wp/v2/posts/21"
}
],
"version-history": [
{
"href": "http://example.com/wp-json/wp/v2/posts/21/revisions"
}
],
"wp:attachment": [
{
"href": "http://example.com/wp-json/wp/v2/media?parent=21"
}
],
"wp:featuredmedia": [
{
"embeddable": true,
"href": "http://example.com/wp-json/wp/v2/media/23"
} …Run Code Online (Sandbox Code Playgroud) 我正在使用WordPress REST API构建密码更改表单.用户输入一个新密码,然后通过AJAX提交给自定义端点,该端点执行以下操作:
$userID = get_current_user_id();
wp_set_password($password, $userID);
//Log user in and update auth cookie
wp_set_current_user($userID);
wp_set_auth_cookie($userID);
//Set the cookie immediately
$_COOKIE[AUTH_COOKIE] = wp_generate_auth_cookie($userID, 2 * DAY_IN_SECONDS);
//Return fresh nonce
return new WP_Rest_Response(array(
'nonce' => wp_create_nonce('wp_rest')
));
Run Code Online (Sandbox Code Playgroud)
端点应自动将用户登录并返回新的nonce,以便他们不必再使用新密码登录.
问题是返回的nonce完全相同且无效.我只能在页面刷新后获得新的nonce.似乎WordPress依赖于生成随机数的一些$_COOKIE或$_SESSION变量在页面刷新之前不会更新.
谢谢你的帮助.
我正在尝试使用wordpress设置反向代理并排除几个路径.我的排除规则适用于admin,includes,.. etc,但它不适用于/ wp-json /.我怀疑是因为.htaccess.我需要wordpress来返回其余的api数据,因为我在gatsbyjs中使用它.
我花了一整天的时间试图解决这个问题.出于某种原因,我的setup/wp-json /返回404,它被代理到我网站的前端部分所在的netlify服务器.如果我删除所有代理规则wp-json的工作原理.
.htaccess包含默认的wordpress内容.
这是我的虚拟主机的pastebin:https: //pastebin.com/vFh6hCkN
<IfModule mod_ssl.c>
<VirtualHost *:443>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive …Run Code Online (Sandbox Code Playgroud) 用wordpress构建的Web API显示错误
<script>document.cookie = "humans_21909=1"; document.location.reload(true)</script>
Run Code Online (Sandbox Code Playgroud)
它有时在某些网络上工作而有时又不工作
我有一个 Gatsby 站点,它使用 GraphQL 从 Wordpress REST API 查询信息以动态创建站点页面。我想将我的索引页设置为动态创建的主页,即home.html
我在 Gatsby CMS 上看到了类似的帖子 如何将关于页面设置为索引页面
但是,他们有一个about.js与他们的关于页面相对应的文件,这意味着他们可以将其导出为一个组件并在索引中使用它,或者他们甚至可以将该文件的内容复制到index.js. 我想设置为我的索引的主页是动态生成的,并且使用了不能在page.js模板之外使用的 GraphQL 查询。所以我没有看到将它复制到另一个文件的简单方法。
我想我的最后一个选择是将我的服务器设置为指向其中的静态文件public/home.html并将其作为站点根目录,但该帖子中的人试图阻止人们这样做。
有任何想法吗?
这是生成站点页面的 page.js 模板:
const PageTemplate = ({ data }) => (
<Layout>
{<h1 dangerouslySetInnerHTML={{ __html: data.currentPage.title }} />}
{
renderBlocks(gatherBlocks(data.currentPage.acf.page_blocks, data))
}
</Layout>
);
export default PageTemplate;
export const pageQuery = graphql`
query ($id: String!) {
currentPage: wordpressPage(id: {eq: $id}) {
title
id
parent {
id
}
template
acf …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用WP REST API获取ID的所有帖子.根据文档,我们可以使用过滤器来使用WP Query参数.使用此帖子结束点将返回所有帖子.
http://demo.wp-api.org/wp-json/wp/v2/posts/?filter[posts__in]=470,469
Run Code Online (Sandbox Code Playgroud) 我可以将整数值传递给WP REST API.但是,不能传递非数字字符.它给出了错误.
这就是我用过的......
add_action( 'rest_api_init', function () {
register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d+)/(?P<username>\d+)', array(
'methods' => 'POST',
'callback' => 'userCheck',
) );
} );
Run Code Online (Sandbox Code Playgroud)
知道如何传递字符串..?
我已经生成了一个消费者密钥和消费者秘密.该网站已安装SSL.我还安装了JSON和REST服务所需的插件.这是网址的样子:
https://<url>/wp-json/wc/v1/products
Run Code Online (Sandbox Code Playgroud)
当我试图通过使用Instagram插件(Chrome插件)使用Basic Auth获取(获取)产品详细信息时,我得到一个JSON响应,如:
{
"code": "woocommerce_rest_cannot_view",
"message": "Sorry, you cannot list resources.",
"data": {
"status": 401
}
}
Run Code Online (Sandbox Code Playgroud)
我同时拥有与Consumer键对应的READ和WRITE权限.
wordpress woocommerce wordpress-rest-api woocommerce-rest-api
我对 Wordpress 的 REST API 中如何处理错误感到有些困惑。在他们的示例中,他们建议使用WP_Error返回错误,但WP_REST_Response将 HTTP 状态代码作为第二个参数,这使得它更短,更符合我的口味。
所以我比较这种返回错误的方式:
return new WP_REST_Response(array('error' => 'Error message.'), 400);
Run Code Online (Sandbox Code Playgroud)
有了这个:
return new WP_Error('rest_custom_error', 'Error message.', array('status' => 400));
Run Code Online (Sandbox Code Playgroud)
使用第一个选项,我可以在响应中只包含错误文本,这对我来说就足够了。所以响应看起来像这样:
{"error":"错误信息。"}
第二个更详细:
{"code":"rest_custom_error","message":"错误消息。","data":{"status":403}}
但是我还需要指定错误代码(第一个参数),这对我的前端实现没有任何好处。除了语法之外,我对性能、安全性和面向未来的因素的差异感到好奇。
那么,除了个人喜好之外,还有什么理由更喜欢其中之一吗?
wordpress ×8
php ×4
gatsby ×2
cookies ×1
http-proxy ×1
javascript ×1
json-api ×1
mod-proxy ×1
nonce ×1
rest ×1
woocommerce ×1
wp-api ×1