[英]WP_query issues using posts_per_pages -1
its a little weird and I need some help or explaination about an issue with the WP_query(); 这有点怪异,我需要一些有关WP_query()问题的帮助或解释;
My code get all post type items with a meta query when you arrive on a specific page: 当您到达特定页面时,我的代码通过meta查询获取所有帖子类型的项目:
echo 'apple';
$args = array(
'post_type' => "family",
'meta_query' => array(
array('key' => '_family')
),
'posts_per_page'=>'9000' // number post in this post type: 18 000 entries
);
$my_query = new WP_Query($args);
echo 'banana';
if i use when page is loaded.. is ok.. i see my "banana" echo. 如果我在页面加载时使用..还可以..我看到我的“香蕉”回声。
If I do this in the "ajax", the value return is "null" and I didn't see my "banana" but I see the "apple" 如果我在“ ajax”中执行此操作,则返回的值为“ null”,并且没有看到“ banana”,但是看到了“ apple”
I have already tried with the posts_per_page=>'3' it works fine in ajax. 我已经尝试过posts_per_page =>'3'在ajax中工作正常。
test: 测试:
posts_per_page=>'3' [OK]
posts_per_page=>'-1' [FAILED]
posts_per_page=>'2000' [FAILED]
Also, we have 4Go on php.ini for memory_limit. 另外,我们在php.ini上有4Go作为memory_limit。
If I run In "safe_mode On" all is ok. 如果我以“ safe_mode On”运行,一切正常。
Any idea? 任何想法?
thank you! 谢谢!
EDIT 编辑
$('body.woocommerce-account #famille-list a').on('click',function (e){
if($(this).data('action') == "edit"){
e.preventDefault();
showProgress();
jQuery.ajax({
type: "post",
dataType: "json",
async: true,
url: scriptParams.ajaxurl,
data: {
action: "edit_family",
id: $(this).data('id')
},
success: function(data) {
if(data != null && data.success == 1){
if($('form.myfamily').css('display')=='none'){
$('form.myfamily').prev().find('h3').click();
}
$('input[name=save_children]').val('Modifier');
$('input[name=id]').val(data.member.id);
hideProgress();
}
}
});
}
});
For AJAX, requests you should only have one echo
statement that is passed along using json_encode
and has a die()
statement afterwards. 对于AJAX,请求只应包含一个使用json_encode
传递的echo
语句,然后再包含die()
语句。 If this doesn't solve your bug, it's at least good practice, since it makes it clear what your HTTP response will be. 如果这不能解决您的错误,则至少是个好习惯,因为它可以清楚地表明您的HTTP响应是什么。
header( "Content-Type: application/json" );
echo json_encode('apple');
die();
// Code should not reach your banana statement
So, for your code you should do this: 因此,对于您的代码,您应该这样做:
$response = array();
$response[] = 'apple';
$args = array(
'post_type' => "family",
'meta_query' => array(
array('key' => '_family')
),
'posts_per_page'=>'9000' // number post in this post type: 18 000 entries
);
$my_query = new WP_Query($args);
$response[] = 'banana';
header( "Content-Type: application/json" );
// will return an array with the value ['apple', 'bannana'];
echo json_encode($response);
die();
Also, are you invoking a PHP file directly? 另外,您是否要直接调用PHP文件? That's a WordPress bad practice. 这是WordPress的不良做法。 There's an established way in which you should handle AJAX requests: http://codex.wordpress.org/AJAX_in_Plugins (It says it's for plugins, but themes can use it too). 有一种已建立的处理AJAX请求的方法: http : //codex.wordpress.org/AJAX_in_Plugins (它说它是用于插件的,但是主题也可以使用它)。 It's a little wordy and hard to get into at first, but it's much cleaner and flexible. 刚开始时有点罗and且难以理解,但更加干净灵活。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.