內容簡介
WP Ajax Query 允許您使用與 WP_Query 相同的查詢參數查詢 WordPress 數據庫,並返回查詢結果的 JSON 表示形式。這使得開發人員可以輕鬆與 WordPress 進行交互,而無需重新學習新的 API。
工作原理
Ajax Query 介面可在 http://example.com/wp-admin/admin-ajax.php?action=query 取得。
一個範例的 jQuery 請求會是這樣:
$.get(ajaxurl, { action: ‘query’ }, function () { }, ‘json’);
查詢一篇文章:
$.get(ajaxurl, { action: ‘query’, p: 1 }, function () { }, ‘json’);
JSON 結果:
{
“id”: 1,
“type”: ‘post’,
“title”: “Hello World”,
“permalink”: “http:\/\/example.com\/?p=1”
}
查詢一個分類:
$.get(ajaxurl, { action: ‘query’, cat: 1 }, function () { }, ‘json’);
JSON 結果:
{
“id”: 1,
“type”: “category”,
“permalink”: “http:\/\/example.com\/?cat=1”,
“terms”: [],
“posts”: []
}
這裡的 terms 和 posts 表示該分類的子分類或屬於該分類的文章的陣列。Why I Created It</h3>
我需要一種在多個插件中查詢文章和分類的常用方式,但沒有找到合適的替代方案。
外掛標籤
開發者團隊
原文外掛簡介
WP Ajax Query allows you to query your WordPress database using the same query paramaters you would use for WP_Query, and return a JSON respresentation of the query results. This allows developers to easily interface with WordPress without having to relearn a new API.
How It Works
The Ajax Query interface would be available at http://example.com/wp-admin/admin-ajax.php?action=query
A sample jQuery request would be like:
$.get(ajaxurl, { action: ‘query’ }, function () { }, ‘json’);
Querying for a post:
$.get(ajaxurl, { action: ‘query’, p: 1 }, function () { }, ‘json’);
JSON results:
{
“id”: 1,
“type”: ‘post’,
“title”: “Hello World”,
“permalink”: “http:\/\/example.com\/?p=1”
}
Query a category:
$.get(ajaxurl, { action: ‘query’, cat: 1 }, function () { }, ‘json’);
JSON results:
{
“id”: 1
“type”: “category”,
“permalink”: “http:\/\/example.com\/?cat=1”,
“terms”: [],
“posts”: []
}
terms and `posts` represents an array of either terms that are sub categories of the category, or posts belonging to the category.
Why I Created It
Needed a common way to query the database for posts and taxonomies across multiple plugins, and didn’t find a suitable replacement.
