0

I have a function being used in an AJAX call for adding the next 5 posts. The call checks the amount of posts being displayed already and send that as an $offset parameter.

The $args for query_posts look like this:

add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore');

function loadMore() {

    $posts_per_page = $_GET['posts_per_page'];
    $offset = $_GET['offset'];

    query_posts( 
        array(
            'posts_per_page' => $posts_per_page,
            'post_type' => 'exhibitor',
            'order' => 'ASC',
            'orderby' => 'title',
            'offset' => $offset
        )
    );

    ob_start();

        get_template_part('exhibitors-post-type/content', 'exhibitor');

    $output = ob_get_contents();
    ob_end_clean();

    wp_reset_query();

    echo $output;

    die();

}

If I change 'order' => 'ASC' to 'order' => 'DESC' then everything works perfectly. It seems that something about 'order' => 'ASC' breaks the 'offset'.

I have tried this with 'orderby' => 'date' and no 'orderby' at all. Both produced the same issue.

UPDATE 09/07/2014 10:32AM ET

This is only happening during an AJAX call. It also only happens on the first request for 5 posts.

The AJAX call and JS that determines the offset is:

$posts_per_page = 5;
query_posts( 
    array(
        'posts_per_page' => $posts_per_page,
        'post_type' => 'exhibitor',
        'order' => 'ASC',
        'orderby' => 'title'
    )
);
global $wp_query; $found_posts = $wp_query->found_posts;
?>

<?php 
get_template_part('exhibitors-post-type/content', 'exhibitor');
?>
<script>

    jQuery(document).ready(function($) {
        var found_posts = <?php echo $found_posts; ?>;
        var posts_per_page = <?php echo $posts_per_page; ?>;
        var posts_on_page = $(".fullarticle").length;       
        $("#load-more").click(function() {
            posts_on_page = $(".fullarticle").length;
            if (posts_on_page === found_posts) {
                $("#load-more").text('No more posts!');
            }
            else {
                $.ajax({
                    type: "GET",
                    url: "<?php echo admin_url('admin-ajax.php'); ?>",
                    dataType: 'html',
                    data: ({ action: 'loadMore', posts_per_page: posts_per_page, offset: posts_on_page}),
                    success: function(results){
                        $('#fullcolumn').append(results);
                        if (posts_on_page === found_posts) {
                            $("#load-more").text('No more!');
                        }
                    }
                });
            }
        });
    });

</script>     

<button id="load-more" name="button">Load More</button>

</div><!-- End articlecontainer -->

<script>
    jQuery(document).ready(function($) {        
        $('.discop-nav-menu-widget-form :checkbox').change(function() {
            var checkValues = $('.discop-nav-menu-widget-form input[type=checkbox]:checked').map(function() {
                return $(this).attr('name');
            }).get();
            $.ajax({
                type: "GET",
                url: "<?php echo admin_url('admin-ajax.php'); ?>",
                dataType: 'html',
                data: { action: 'exhibitor_ajax', ids: checkValues },
                success:function(result){
                    $('#the-results').empty();
                    $('#the-results').html(result);
                }
            });
        });
    });
</script>
2
  • You must know that query_posts() should not be used by plugins or themes because, most of the time, it alters the main query when it has been already executed. Use new WP_Query or get_posts() instead. Check using on of these functions and tell if it works. If not, please post the full code you are using, php and javascript.
    – cybmeta
    Sep 7, 2014 at 13:48
  • @cybmeta, I updated my question with a little more information. I am modifying an existing theme that was using query_posts. I am aware of the common issues with this and attempted to change this to a new WP_query however that resulted in the AJAX call returning a 500 internal server error. Sep 7, 2014 at 14:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.