我遇到了一点问题。
我有一个函数叫做get_press()
, 它检索最新的新闻项目。它位于插件内部:
class EWPress {
function __construct()
{
load_plugin_textdomain( \'ew\', flase, dirname( plugin_basename( __FILE__ ) ) . \'/lang\' );
add_action( \'wp_enqueue_scripts\', array( &$this, \'register_plugin_scripts\' ) );
// Add JS var ajaxurl to global namespace
add_action( \'wp_head\', array( &$this, \'add_ajax_library\' ) );
add_action( \'wp_ajax_get_press\', array( &$this, \'get_press\' ) );
add_action( \'wp_ajax_nopriv_get_press\', array( &$this, \'get_press\' ) );
}
public function get_press()
{
$args = array(
\'status\' => \'publish\',
\'post_type\' => \'press\',
\'orderby\' => \'meta_value\',
\'meta_key\' => \'press_date\',
\'order\' => \'DESC\',
\'posts_per_page\' => 2
);
$query = new WP_Query($args);
return($query->posts);
//echo json_encode($query->posts);
//die();
}
}
如果我通过以下方式在模板文件中直接调用此函数:
echo EWPress::get_press()
输出完全正常:
Array
(
[0] => WP_Post Object
(
[ID] => 229
[post_author] => 1
[post_date] => 2013-01-18 00:29:58
[post_date_gmt] => 2013-01-18 00:29:58
[post_content] =>
[post_title] => Rundown
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => rundown
[to_ping] =>
[pinged] =>
[post_modified] => 2013-01-18 00:29:58
[post_modified_gmt] => 2013-01-18 00:29:58
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://site.com/?post_type=press&p=229
[menu_order] => 0
[post_type] => press
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => WP_Post Object
(
[ID] => 231
[post_author] => 1
[post_date] => 2013-01-18 00:44:35
[post_date_gmt] => 2013-01-18 00:44:35
[post_content] =>
[post_title] => Clean Plates
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => clean-plates
[to_ping] =>
[pinged] =>
[post_modified] => 2013-01-18 00:44:35
[post_modified_gmt] => 2013-01-18 00:44:35
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://site.com/?post_type=press&p=231
[menu_order] => 0
[post_type] => press
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
为了发出AJAX请求,我:
注释掉echo EWPress::get_press()
在模板级别上,注释掉return $query->posts
在get_press()
功能取消注释echo json_encode($query->posts)
取消对die();
但是,当我向同一个函数发出AJAX请求时,返回的数据是不同的(即忽略它order = \'ASC\'
或\'DESC
).
AJAX功能:
(function($) {
$(document).ready(function(){
$.ajax({
url: ajaxurl,
type: \'POST\',
dataType: \'json\',
data: {action: \'get_press\'},
complete: function(xhr, textStatus) {
//
},
success: function(data, textStatus, xhr) {
console.log(data);
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
});
})(jQuery);
控制台。日志:
[Object, Object]
0: Object
ID: 234
comment_count: "0"
comment_status: "closed"
filter: "raw"
guid: "http://site.com/?post_type=press&p=234"
menu_order: 0
ping_status: "closed"
pinged: ""
post_author: "1"
post_content: ""
post_content_filtered: ""
post_date: "2013-01-18 02:33:41"
post_date_gmt: "2013-01-18 02:33:41"
post_excerpt: ""
post_mime_type: ""
post_modified: "2013-01-18 02:33:41"
post_modified_gmt: "2013-01-18 02:33:41"
post_name: "eater"
post_parent: 0
post_password: ""
post_status: "publish"
post_title: "Eater"
post_type: "press"
to_ping: ""
__proto__: Object
1: Object
ID: 231
comment_count: "0"
comment_status: "closed"
filter: "raw"
guid: "http://site.com/?post_type=press&p=231"
menu_order: 0
ping_status: "closed"
pinged: ""
post_author: "1"
post_content: ""
post_content_filtered: ""
post_date: "2013-01-18 00:44:35"
post_date_gmt: "2013-01-18 00:44:35"
post_excerpt: ""
post_mime_type: ""
post_modified: "2013-01-18 00:44:35"
post_modified_gmt: "2013-01-18 00:44:35"
post_name: "clean-plates"
post_parent: 0
post_password: ""
post_status: "publish"
post_title: "Clean Plates"
post_type: "press"
to_ping: ""
__proto__: Object
length: 2
__proto__: Array[0]
有人知道为什么会这样吗?