使用AJAX时WP_QUERY ORDERBY中断?

时间:2013-01-18 作者:tr3online

我遇到了一点问题。

我有一个函数叫做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->postsget_press() 功能取消注释echo json_encode($query->posts)die();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]
有人知道为什么会这样吗?

4 个回复
SO网友:Matthew Boynes

我相信米洛是对的,这可能是一个过滤器的影响。Ajax请求看起来像管理员请求(即,is_admin() 返回true),因此有可能正在检查is_admin() 并在这些情况下添加过滤器。

您可以设置\'suppress_filters\' => true 在args数组中,它可能会起作用。请注意,这样做可能会阻止缓存查询,或者阻止插件对查询执行所需的操作。这可能不会影响你,但仍然需要记住。

SO网友:selim13

pre_get_posts 操作也会影响查询参数。您可以尝试添加remove_all_actions(\'pre_get_posts\'); 之前的ajax处理程序WP_Query.如果这有帮助,请使用检查操作

global $wp_filter
print_r($wp_filter[\'pre_get_posts\']);
查看什么可以破坏WP\\U查询行为。在我的例子中,在ajax模式下,影响WP\\U查询的是简单的自定义发布顺序。

SO网友:tr3online

出于某种奇怪的原因WP_Query 是这个问题背后的罪魁祸首。

对返回WP_Query 对象$query->posts 对象完全忽略中提供的任何参数$args.

一个简单的解决方法是get_posts 相反,使用相同的$argsWP_Query

这种方法的一个不幸缺点是丢失了WP_query 对象,例如$query->max_num_pages.

SO网友:MathieuF
\'suppress_filters\' => true

This work for me!

结束

相关推荐

How does admin-ajax.php work?

我们与外部开发人员有一些问题。我们想限制访问wp-admin 仅限现场内部访问(通过VPN). 这样就不会受到外部用户的攻击。我们可以从站点中枚举管理员,不希望他们被钓鱼。我们的开发人员说我们不能这样做,因为网站需要有外部访问的管理页面,这样页面才能正常工作。特别是admin-ajax 页什么是admin-ajax.php 页码do?它位于WordPress的管理部分。最终用户是否未经身份验证就访问?让外部用户使用此功能是否不安全?