简体   繁体   中英

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();

My code get all post type items with a meta query when you arrive on a specific page:

     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"

I have already tried with the posts_per_page=>'3' it works fine in 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.

If I run In "safe_mode On" all is ok.

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. If this doesn't solve your bug, it's at least good practice, since it makes it clear what your HTTP response will be.

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? That's a WordPress bad practice. 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). It's a little wordy and hard to get into at first, but it's much cleaner and flexible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM